我有一个奇怪的(?)问题。我正在制作一个简单的GUI,当我添加一个JTextField(注释)时,一些组件消失了。我不知道为什么。此外,有时当我玩我的GUI时,一些组件随机消失(第一次播放,一些按钮消失,第二次播放 - 一些标签消失)这太奇怪了!有什么想法吗?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class matchWindow extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField tAttack;
public matchWindow(){
setTitle("Match");
setSize(960, 720);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
JLabel lPlayers = new JLabel("Players list");
lPlayers.setBounds(50, 10, 100, 30);
lPlayers.setVisible(true);
add(lPlayers);
JLabel lAction = new JLabel("Action list");
lAction.setBounds(250, 10, 100, 30);
lAction.setVisible(true);
add(lAction);
JLabel lStats = new JLabel("Player statistics");
lStats.setBounds(650, 10, 100, 30);
lStats.setVisible(true);
add(lStats);
JLabel lAttack = new JLabel("Attack");
lAttack.setBounds(175, 45, 50, 30);
lAttack.setVisible(true);
add(lAttack);
JLabel lAttackPrec = new JLabel("Attack");
lAttackPrec.setBounds(550, 45, 50, 30);
lAttackPrec.setVisible(true);
add(lAttackPrec);
//tAttack = new JTextField();
//tAttack.setBounds(550, 75, 50, 30);
//tAttack.setVisible(true);
//add(tAttack);
JLabel lDefense = new JLabel("Defense");
lDefense.setBounds(230, 45, 60, 30);
lDefense.setVisible(true);
add(lDefense);
JLabel lDefensePrec = new JLabel("Defense");
lDefensePrec.setBounds(620, 45, 60, 30);
lDefensePrec.setVisible(true);
add(lDefensePrec);
JLabel lServe = new JLabel("Serve");
lServe.setBounds(300, 45, 50, 30);
lServe.setVisible(true);
add(lServe);
JLabel lServePrec = new JLabel("Serve");
lServePrec.setBounds(690, 45, 50, 30);
lServePrec.setVisible(true);
add(lServePrec);
JLabel lBlock = new JLabel("Block");
lBlock.setBounds(355, 45, 50, 30);
lBlock.setVisible(true);
add(lBlock);
JButton bY1 = new JButton("Y");
bY1.setBounds(160, 75, 25, 25);
bY1.setVisible(true);
add(bY1);
JButton bN1 = new JButton("N");
bN1.setBounds(190, 75, 25, 25);
bN1.setVisible(true);
add(bN1);
JButton bY2 = new JButton("Y");
bY2.setBounds(225, 75, 25, 25);
bY2.setVisible(true);
add(bY2);
JButton bN2 = new JButton("N");
bN2.setBounds(255, 75, 25, 25);
bN2.setVisible(true);
add(bN2);
JButton bY3 = new JButton("Y");
bY3.setBounds(290, 75, 25, 25);
bY3.setVisible(true);
add(bY3);
JButton bN3 = new JButton("N");
bN3.setBounds(320, 75, 25, 25);
bN3.setVisible(true);
add(bN3);
JButton bY4 = new JButton("Y");
bY4.setBounds(350, 75, 25, 25);
bY4.setVisible(true);
add(bY4);
JButton bN4 = new JButton("N");
bN4.setBounds(380, 75, 25, 25);
bN4.setVisible(true);
add(bN4);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
matchWindow window = new matchWindow();
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class matchWindow extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField tAttack;
public matchWindow(){
setTitle("Match");
setSize(960, 720);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
JLabel lPlayers = new JLabel("Players list");
lPlayers.setBounds(50, 10, 100, 30);
lPlayers.setVisible(true);
add(lPlayers);
JLabel lAction = new JLabel("Action list");
lAction.setBounds(250, 10, 100, 30);
lAction.setVisible(true);
add(lAction);
JLabel lStats = new JLabel("Player statistics");
lStats.setBounds(650, 10, 100, 30);
lStats.setVisible(true);
add(lStats);
JLabel lAttack = new JLabel("Attack");
lAttack.setBounds(175, 45, 50, 30);
lAttack.setVisible(true);
add(lAttack);
JLabel lAttackPrec = new JLabel("Attack");
lAttackPrec.setBounds(550, 45, 50, 30);
lAttackPrec.setVisible(true);
add(lAttackPrec);
tAttack = new JTextField();
tAttack.setBounds(500, 75, 50, 30);
tAttack.setVisible(true);
add(tAttack);
JLabel lDefense = new JLabel("Defense");
lDefense.setBounds(230, 45, 60, 30);
lDefense.setVisible(true);
add(lDefense);
JLabel lDefensePrec = new JLabel("Defense");
lDefensePrec.setBounds(620, 45, 60, 30);
lDefensePrec.setVisible(true);
add(lDefensePrec);
JLabel lServe = new JLabel("Serve");
lServe.setBounds(300, 45, 50, 30);
lServe.setVisible(true);
add(lServe);
JLabel lServePrec = new JLabel("Serve");
lServePrec.setBounds(690, 45, 50, 30);
lServePrec.setVisible(true);
add(lServePrec);
JLabel lBlock = new JLabel("Block");
lBlock.setBounds(355, 45, 50, 30);
lBlock.setVisible(true);
add(lBlock);
JButton bY1 = new JButton("Y");
bY1.setBounds(160, 75, 25, 25);
bY1.setVisible(true);
add(bY1);
JButton bN1 = new JButton("N");
bN1.setBounds(190, 75, 25, 25);
bN1.setVisible(true);
add(bN1);
JButton bY2 = new JButton("Y");
bY2.setBounds(225, 75, 25, 25);
bY2.setVisible(true);
add(bY2);
JButton bN2 = new JButton("N");
bN2.setBounds(255, 75, 25, 25);
bN2.setVisible(true);
add(bN2);
JButton bY3 = new JButton("Y");
bY3.setBounds(290, 75, 25, 25);
bY3.setVisible(true);
add(bY3);
JButton bN3 = new JButton("N");
bN3.setBounds(320, 75, 25, 25);
bN3.setVisible(true);
add(bN3);
JButton bY4 = new JButton("Y");
bY4.setBounds(350, 75, 25, 25);
bY4.setVisible(true);
add(bY4);
JButton bN4 = new JButton("N");
bN4.setBounds(380, 75, 25, 25);
bN4.setVisible(true);
add(bN4);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
matchWindow window = new matchWindow();
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
你正在编写组件那种覆盖的方式我修复了你的代码问题
答案 1 :(得分:0)
首先添加组件,而不是让框架可见。在构造函数中,你有一个setVisible(true)删除它。 OFFTOPIC:并使用Layoutmanagers而不是“Null-Layout”。