public ButtonGrid(int width, int length){ //constructor
frame.setTitle("MPC");
frame.setLayout(new GridLayout(width,length)); //set layout
grid=new JButton[width-1][length]; //allocate the size of grid
for(int y=0; y<length; y++){
for(int x=0; x<width-1; x++){
grid[x][y]=new JButton("("+x+","+y+")"); //creates new button
frame.add(grid[x][y]); //adds button to grid
grid[x][y].addActionListener(this);
grid[x][y].addKeyListener(this);
//grid[x][y].setMnemonic(KeyEvent.VK_0);
grid[x][y].setPreferredSize(new Dimension(75,75));
}
}
for(int i =0; i<boxList.length; i++)
box.addItem(boxList[i]);
box.addActionListener(this);
frame.add(box);
frame.add(met_speed);
frame.add(Play);
Play.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
public void newWindow(){
JFrame frame1 = new JFrame();
frame1.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextField[] btn = new JTextField[12];
JLabel[] lbl = new JLabel[12];
JLabel name = new JLabel("Preset Name");
JTextField name1 = new JTextField();
name1.setPreferredSize(new Dimension(100,25));
gbc.gridx = 0;
gbc.gridy = 0;
frame1.add(name, gbc);
gbc.gridx++;
frame1.add(name1, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
for(int i = 0; i<12; i++){
lbl[i] = new JLabel("Button" + (i+1));
frame1.add(lbl[i], gbc);
gbc.gridy++;
}
gbc.gridx = 1;
gbc.gridy = 1;
for(int i = 0; i<12; i++){
btn[i] = new JTextField();
btn[i].setPreferredSize(new Dimension(75,25));
frame1.add(btn[i], gbc);
gbc.gridy++;
}
gbc.gridx = 0;
gbc.gridy = 14;
JButton save = new JButton("Save");
frame1.add(save, gbc);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack(); //sets appropriate size for frame
frame1.setVisible(true); //makes frame visible
}
第一个函数ButtonGrid在main中调用,包含真实程序。按下按钮后,newWindow()将被调用为弹出窗口。到目前为止,它工作正常,但当我关闭frame1时,它关闭框架并结束程序。
我这样做是正确的还是我需要添加的东西?
答案 0 :(得分:5)
最佳解决方案:不要弹出&#34;一个JFrame。使用JDialog,因为关闭它不会关闭你的程序,这正是他们所做的。是的,您可以将JFrame的defaultCloseOperation更改为DO_NOTHING_ON_CLOSE,但您仍然在使用主程序窗口来执行它不应该执行的操作。最好使用正确的工具,JDialog。另请注意,JDialog可以采用您当前在弹出的#34;中提供的任何复杂的GUI。的JFrame。
例如:
import java.awt.Dialog.ModalityType;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwoWindows {
public static final int PREF_W = 600;
public static final int PREF_H = 400;
private static void createAndShowGui() {
final JFrame frame = new JFrame("Main Application");
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
buttonPanel.add(new JButton(new AbstractAction("New JFrame") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_F);
}
@Override
public void actionPerformed(ActionEvent e) {
JFrame frame2 = new JFrame("Frame 2");
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setupAndDisplay(frame2);
}
}));
buttonPanel.add(new JButton(new AbstractAction("New Modeless JDialog") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_D);
}
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(frame, "Modeless Dialog", ModalityType.MODELESS);
setupAndDisplay(dialog);
}
}));
buttonPanel.add(new JButton(new AbstractAction("New Modal JDialog") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_M);
}
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(frame, "Modal Dialog", ModalityType.APPLICATION_MODAL);
setupAndDisplay(dialog);
}
}));
JPanel mainPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
};
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.add(buttonPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private static void setupAndDisplay(Window window) {
window.add(Box.createRigidArea(new Dimension(200, 100)));
window.pack();
window.setLocationByPlatform(true);
window.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
请运行此选项以注意两种顶级窗口之间的行为差异。:
答案 1 :(得分:2)
你可以试试这个:
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//<-- destroy only this frame