ボタンをクリックしてクリックするだけで多くのJFrameを基本的に更新するアプリケーションがあります。増分をクリックすると、すべての属性のすべての値が増分されます。ただし、コントローラークラスからメインクラスのコンストラクターにパラメーターを渡す必要があります。これにより、現在、各Jframeがそれぞれ上に束ねられているため、各Jframeに異なる場所が与えられます。どうすればこれを行うことができますか?
コントローラクラス
public class Controller extends JFrame
implements ActionListener {
private Model model;
private View3 view3;
private View4 view4;
private JButton clearViews; // For direct message to views
private JButton incB;
private String title;
// Constructor
public Controller(Model model, String title) {
// Record reference to the model
this.model = model;
this.title = getTitle();
// Configure the window
setTitle("Controller");
setLocation(40,200);
setSize(350,150);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout()); // The default is that JFrame uses BorderLayout
// Set up input GUI
clearViews = new JButton("Clear views");
window.add(clearViews);
clearViews.addActionListener(this);
incB = new JButton("Increment B");
window.add(incB);
incB.addActionListener(this);
view3 = new View3(this, model);
window.add(view3);
view4 = new View4(this, model);
window.add(view4);
// Display the frame
setVisible(true);
} // constructor
// Button click handling:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearViews) {
view3.clear();
view4.clear();
}
else if (e.getSource() == incB)
model.modifyB(); // The model will trigger the views to update themselves
} // actionPerformed
}
主な方法
public class Main {
public static void main(String args[]) {
Model model = new Model();
String title = new String();
Controller1 c1 = new Controller(model, title);
Controller2 c2 = new Controller(model, title);
Controller2 c3 = new Controller(model, title );
} // main
} // Main
一般的な方法でそれを行うことはあまりありません。さらに2つのパラメーターを受け入れ、それらを現在地に使用するだけです。
public Controller(Model model, String title, int xPos, int yPos) {
// Record reference to the model
this.model = model;
this.title = getTitle();
// Configure the window
setTitle("Controller");
setLocation(xPos, yPos);
// Rest of the constructor...
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加