表JAVA GUI没有输出
你好,我在找人帮我解决这个问题。这张桌子不想超过。有什么办法可以解决这个问题吗表JAVA GUI没有输出,java,Java,你好,我在找人帮我解决这个问题。这张桌子不想超过。有什么办法可以解决这个问题吗 public class GameStart extends JFrame{ private JPanel p1,p2; private JButton boom1,boom2; private ImageIcon image[] = {new ImageIcon("build/image/scissors.png"),new ImageIcon("build/image/pa
public class GameStart extends JFrame{
private JPanel p1,p2;
private JButton boom1,boom2;
private ImageIcon image[] = {new ImageIcon("build/image/scissors.png"),new ImageIcon("build/image/paper.png"),
new ImageIcon("build/image/rock.png"),new ImageIcon("build/image/spock.png"),new ImageIcon("build/image/lizard.png")};
private JTable table;
private String score[] = {"0","1"};
private String total;
public GameStart(String title)
{
setTitle(title); //set the title
setBounds(100,100,800,500);
p1 = new JPanel(); //create panel
p1.setBorder(new EmptyBorder(5,5,5,5));
setContentPane(p1);
setSize(900,900);
p1.setLayout(null);
setVisible(true); //make it visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel gametitle = new JLabel("Rock-Paper-Scissors-Lizard-Spock"); //Title
gametitle.setHorizontalAlignment((SwingConstants.CENTER));
gametitle.setBounds(1,11,781,14);
p1.add(gametitle);
JPanel titlecolor = new JPanel(); //background color for title
titlecolor.setBackground(Color.orange);
titlecolor.setBounds(0,0,900,30);
p1.add(titlecolor);
JLabel Rounds = new JLabel("Round 1"); // text for rounds
Rounds.setHorizontalAlignment(SwingConstants.CENTER);
Rounds.setBounds(0,80,782,14);
Rounds.setFont(new Font("Verdana",Font.BOLD,15));
p1.add(Rounds);
JLabel player1name = new JLabel ("Player 1");//Name for player 1
player1name.setHorizontalAlignment(SwingConstants.CENTER);
player1name.setBounds(0,300,200,14);
p1.add(player1name);
JLabel picture1 = new JLabel(image[0]); //Image for player 1
picture1.setHorizontalAlignment(SwingConstants.CENTER);
picture1.setBounds(50,250,400,200);
p1.add(picture1);
JLabel player2name = new JLabel ("Player 2");// Name for player2
player2name.setHorizontalAlignment(SwingConstants.CENTER);
player2name.setBounds(650,300,200,14);
p1.add(player2name);
JLabel picture2 = new JLabel(image[3]);//Image for player 2
picture2.setHorizontalAlignment(SwingConstants.CENTER);
picture2.setBounds(400,250,400,200);
p1.add(picture2);
boom1 = new JButton("BOOM!"); //Boom Button for player 1
boom1.setHorizontalAlignment(SwingConstants.CENTER);
boom1.setBounds(700,350,80,16);
p1.add(boom1);
boom2 = new JButton("BOOM!"); //boom button for player 2
boom2.setHorizontalAlignment(SwingConstants.CENTER);
boom2.setBounds(50,350,80,16);
p1.add(boom2);
JLabel scissors = new JLabel(image[0]); //scissors image
JLabel paper = new JLabel (image[1]); // paper image
JLabel rock = new JLabel (image[2]); // rock image
JLabel spock = new JLabel (image[3]);// spock image
JLabel lizard = new JLabel (image[4]);// lizard image
JLabel winscore = new JLabel(score[0]);
JLabel losescore = new JLabel(score[1]);
JLabel totalscore = new JLabel (total);
String [][] playername = {{"Team 1:"+ player1name,""+winscore,""+losescore,""+winscore,""+totalscore},{"Team 2:"+ player2name,""+score[1],""+score[1],""+score[1],""+totalscore}};//create row for table
String [] column = {"Player","Round 1","Round 2","Round 3","Total"}; //create column for table
table = new JTable(playername,column); //set the table with row and column variables.
table.setPreferredScrollableViewportSize(new Dimension(500,50));
table.setFillsViewportHeight(true);
table.setBounds(50,700,80,16);
JScrollPane sp = new JScrollPane(table);
p1.add(sp);
我的桌子有问题。它不执行表部分。
请有人帮我解决这个问题。
我已经在互联网上找到了解决方案,但仍然是一样的。简而言之,不要使用空布局管理器(
p1.setLayout(null);
)
否则,由于您没有使用任何布局管理器,因此必须设置每个组件的位置和大小-滚动窗格不需要这样做。添加如下内容:
...
JScrollPane sp = new JScrollPane(table);
sp.setBounds(10,10,200,100); // values MUST be changed as needed
p1.add(sp);
由于框架已经可见,请在添加所有组件后调用validate()
:
...
p1.add(sp);
validate();
不要使用空布局!!!Swing设计用于布局管理器。