[英]Duplication of JButtons in JFrame
I have created a "Main Menu" JFrame with different JButtons for different periods. 我创建了一个“主菜单” JFrame,并在不同的时期使用了不同的JButton。 If I click on one of the buttons a new JFrame opens up but whenever I dispose of it and go back to the main menu and open it back up it it seems as if it more buttons and I don't know why. 如果我单击其中一个按钮,则会打开一个新的JFrame,但是每当我处理掉它并返回主菜单并重新打开它时,似乎又有更多按钮,我不知道为什么。
Main Menu
public class GUI extends JFrame implements ActionListener {
public static final int WIDTH=1000;
public static final int LENGTH=900;
public static JFrame myFrame = new JFrame("TOK Discussion Participation");
static JPanel myPanel = new JPanel();
public static JComponent buttonPanel;
public static JPanel IPanel = new JPanel();
public static JFrame Period7 = new JFrame("Period 7");
JPanel myPanel1 = new JPanel();
public static Object createPanelPERIOD7;
public JFrame getMyFrame()
{
return myFrame;
}
public void setWindow()
{
//// Creates Icon in frame
JLabel Image = new JLabel();
ImageIcon myIcon = new ImageIcon(new ImageIcon("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg").getImage().getScaledInstance(200, 200, 1));
Image.setIcon(myIcon);
//set window
Color backgroundColor = new Color(5,149,251);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setBounds(550,200,1000,600);
myFrame.getContentPane().setBackground(Color.LIGHT_GRAY);
myFrame.setLayout(new BorderLayout());
myFrame.add(myPanel1,BorderLayout.NORTH);
myFrame.add(new JLabel(new ImageIcon("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg")), BorderLayout.CENTER);
//myFrame.setIconImage("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg");
myFrame.getContentPane().setBackground(backgroundColor);
myFrame.setVisible(true);
//set label
JLabel myLabel = new JLabel("TOK Discussion Participation");
JLabel myLabel1= new JLabel("");
JLabel myLabel2 = new JLabel("");
//set button (PERIOD 7)
JButton Period_7_BUTTON = new JButton("Period 7");
//set Listener (PERIOD 7)
Period_7_BUTTON.addActionListener(new GUI());
//2nd button (PERIOD 5)
JButton Period_5_BUTTON = new JButton("Period 5");
//set Listener PERIOD 5)
Period_5_BUTTON.addActionListener(new GUI());
//2nd button (PERIOD 2)
JButton Period_2_BUTTON = new JButton("Period 2");
//set Listener PERIOD 2)
Period_2_BUTTON.addActionListener(new GUI());
//2nd button (PERIOD 2)
JButton Period_4_BUTTON = new JButton("Period 4");
//set Listener PERIOD 4)
Period_4_BUTTON.addActionListener(new GUI());
//Setting Button FONT/SIZE
Period_2_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
Period_4_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
Period_5_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
Period_7_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
//Bundle button to panel
myPanel1.setLayout(new BoxLayout(myPanel1, BoxLayout.X_AXIS));
myPanel1.add(Period_7_BUTTON);
myPanel1.add(Period_5_BUTTON);
myPanel1.add(Period_2_BUTTON);
myPanel1.add(Period_4_BUTTON);
//add content to window and make it visible
myFrame.add(myPanel1,BorderLayout.NORTH);
myFrame.setVisible(true);
}
public void dispose(){
myFrame.dispose();
}
Other JFrame that duplicates buttons once reopened 一旦重新打开,其他复制按钮的JFrame
//Method to create Period 7 Frame
public static void createFramePERIOD7() {
//Creates Panel + Buttons
JPanel P7_Panel = new JPanel();
JButton Column = new JButton("Add Columns");
JButton Insert = new JButton("Insert Student 7");
JButton Back = new JButton("Home");
//Adding Buttons and Layout to Period 7 Frame
Period7.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Period7.setBounds(550,200,1000,600);
Period7.setLayout(new FlowLayout());
Period7.add(Back);
Period7.add(Column);
Period7.add(Insert);
//Add ActionListener to Buttons
Back.addActionListener(new GUI());
Insert.addActionListener(new GUI());
Column.addActionListener(new GUI());
//Adding Panel to Frame
//Period7.add(P7_Panel);
Period7.setVisible(true);
}
And finally my ending listener code 最后是我的结尾侦听器代码
public void actionPerformed(ActionEvent e)
{
String buttonCommand = e.getActionCommand();
//Main menu Period 7 button
if(buttonCommand.equals("Period 7"))
{
myFrame.dispose();
createFramePERIOD7();
}
if(buttonCommand.equals("Home"))
{
setWindow();
Period7.dispose();
}
Your method adds 3 buttons everytime... You can fix it by changing your 您的方法每次都会添加3个按钮...您可以通过更改
createFramePERIOD7() method
By inserting the line indicated below. 通过插入下面指示的行。
public static void createFramePERIOD7() {
//Creates Panel + Buttons
JPanel P7_Panel = new JPanel();
JButton Column = new JButton("Add Columns");
JButton Insert = new JButton("Insert Student 7");
JButton Back = new JButton("Home");
Period7 = new JFrame(); //<-------------------------- Insert this line
//Adding Buttons and Layout to Period 7 Frame
Period7.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Period7.setBounds(550,200,1000,600);
Period7.setLayout(new FlowLayout());
Period7.add(Back);
Period7.add(Column);
Period7.add(Insert);
//Add ActionListener to Buttons
Back.addActionListener(new GUI());
Insert.addActionListener(new GUI());
Column.addActionListener(new GUI());
//Adding Panel to Frame
//Period7.add(P7_Panel);
Period7.setVisible(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.