gpt4 book ai didi

java - 单击按钮时应该打开新窗口吗?

转载 作者:搜寻专家 更新时间:2023-10-31 19:58:51 25 4
gpt4 key购买 nike

我知道这是一个非常简单的问题,但我找不到解决方案。

我有一个主 Swing 对话框和其他 Swing 对话框。主对话框有一个按钮。单击按钮后如何打开另一个对话框?

编辑:

当我尝试这样做时:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
NewJDialog okno = new NewJDialog();
okno.setVisible(true);
}

我得到一个错误:

Cannot find symbol NewJDialog

第二个窗口名为 NewJDialog...

最佳答案

您肯定想看看 How to Make Dialogs并查看 JDialog应用程序接口(interface)。这是一个入门的简短示例。您可以将它与您现在正在做的事情进行比较。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class DialogTest extends JDialog implements ActionListener {

private static final String TITLE = "Season Test";

private enum Season {
WINTER("Winter"), SPRING("Spring"), SUMMER("Summer"), FALL("Fall");
private JRadioButton button;
private Season(String title) {
this.button = new JRadioButton(title);
}
}

private DialogTest(JFrame frame, String title) {
super(frame, title);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(0, 1, 8, 8));
ButtonGroup group = new ButtonGroup();
for (Season s : Season.values()) {
group.add(s.button);
radioPanel.add(s.button);
s.button.addActionListener(this);
}
Season.SPRING.button.setSelected(true);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.add(radioPanel);
this.pack();
this.setLocationRelativeTo(frame);
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
JRadioButton b = (JRadioButton) e.getSource();
JOptionPane.showMessageDialog(null, "You chose: " + b.getText());
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new DialogTest(null, TITLE);
}
});
}
}

关于java - 单击按钮时应该打开新窗口吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2663199/

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