gpt4 book ai didi

java - 使用另一个面板的值刷新 JPanel

转载 作者:行者123 更新时间:2023-12-02 05:28:29 24 4
gpt4 key购买 nike

我想返回第一个 JPanel 中设置的值,并通过单击 RYSUJ 按钮使用它们重新绘制第二个 JPanel。我需要一个简单的解决方案来发送参数:

我的主要类(class):

    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.
}

}

最佳答案

忘记 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 的逻辑。允许 View 类(GUI 类)在模型上注册监听器,然后当一个类更改模型时,所有监听器都会收到通知,并可以查询模型的状态,然后相应地调整其 View 。

关于java - 使用另一个面板的值刷新 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25776058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com