我想返回第一个JPanel中设置的值,并使用它们通过单击RYSUJ按钮重新绘制第二个JPanel。我需要一个简单的解决方案来发送参数:
我的主要课程:
public static void main(String[] args) throws IOException {
FractalFrame gui = new FractalFrame();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
框架:
public class FractalFrame extends JFrame {
public FractalFrame() {
setTitle("Fractalz beta");
setSize(800, 600);
setResizable(false);
FractalzGUI gui = new FractalzGUI();
FractalJPanel panel = new FractalJPanel(gui.kol, gui.zoom, gui.radio);
this.add(panel);
this.add(gui, BorderLayout.EAST);
}
}
gui类(它是第一个JPanel):
public class FractalzGUI extends JPanel implements ActionListener {
private JRadioButton mandelbrotRadio = new JRadioButton("Zbiór Mandelbrota");
private JRadioButton shipRadio = new JRadioButton("Płonący statek");
private JLabel ustawZoom = new JLabel("zoom: ");
private JTextField zoomtf = new JTextField("", 5);
private JLabel wyborKoloru = new JLabel("Wybierz kolor: ");
private String[] kolory = {"zolty", "niebieski", "czerwony", "zielony", "brazowy", "fioletowy"};
private JComboBox listaKolorow = new JComboBox(kolory);
private JButton rysuj = new JButton("Rysuj!");
public int radio=0; //1 = mandelbrot, 2 = ship
public int zoom=0;
public int kol=0;
public FractalzGUI() {
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(mandelbrotRadio, gbc);
gbc.gridy = 1;
this.add(shipRadio, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
this.add(ustawZoom, gbc);
gbc.gridx = 1;
this.add(zoomtf, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
this.add(wyborKoloru, gbc);
gbc.gridx = 1;
this.add(listaKolorow, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
this.add(rysuj, gbc);
mandelbrotRadio.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
shipRadio.setSelected(false);
}
});
shipRadio.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mandelbrotRadio.setSelected(false);
}
});
rysuj.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (mandelbrotRadio.isSelected()) {
radio = 1;
} else if (shipRadio.isSelected()) {
radio = 2;
}
if(zoomtf.getText().equals("")) zoom=0;
else zoom=Integer.parseInt(zoomtf.getText());
switch(listaKolorow.getSelectedIndex()){
case 0:
kol = 40;
break;
case 1:
kol = 165;
break;
case 2:
kol = 256;
break;
case 3:
kol = 105;
break;
case 4:
kol = 20;
break;
case 5:
kol = 200;
break;
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
答案 0 :(得分:2)
忘记Swing,忘记GUI了。一个对象更改另一个对象状态的最简单方法是让第一个对象调用第二个方法,将信息传递给它。你应该有一个类持有另一个的实例,然后根据需要让第二个调用方法在第二个传递信息。这可以在FractalFrame构造函数中设置,方法是将一个类的实例传入另一个类。
如果需要,你甚至可以将两者都传递给对方,但这会使耦合收紧很多。 即,
FractalzGUI gui = new FractalzGUI();
FractalJPanel panel = new FractalJPanel(gui.kol, gui.zoom, gui.radio);
// obviously these classes need setter methods
gui.setFractalJPanel(panel);
this.add(panel);
this.add(gui, BorderLayout.EAST);
修改强>
被专业人士和纯粹主义者召唤......
更好的是,使用一个单独的非GUI模型类来保存GUI的基础逻辑。允许视图类(GUI类)在模型上注册侦听器,然后当一个类更改模型时,将通知所有侦听器并可以查询模型的状态,然后相应地调整其视图。