gpt4 book ai didi

java - 添加 JTextField 后 GUI 损坏

转载 作者:行者123 更新时间:2023-12-01 13:26:21 26 4
gpt4 key购买 nike

我有一个奇怪的(?)问题。我正在制作一个简单的 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

}

}

最佳答案

    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

}

}

你正在重写组件,这样就重写了我解决了你的代码的问题

关于java - 添加 JTextField 后 GUI 损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795681/

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