你好,下午好,
我对Java GUI编程非常陌生,这是我的第一个项目。我目前正在制作一个记分板GUI,以显示用户分数,一些问题等。我一直在使用NetBeans GUI Builder,并尝试适当地调整应用程序的大小。
理想情况下,当我按“ Build and Compile”并运行该程序时,我希望填充这张图片中列出的内容:
我尝试查看: https://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html Resizing JPanel Form in NetBeans GUI Builder
并没有任何答案可以使用。有人可以帮我吗?我已经待了四个小时了,我真的很希望找到一个解决方案,以便我可以学习并继续发展我的Java GUI技能组。下面是项目的代码。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Kelly
*/
public class Scoreboard extends javax.swing.JFrame {
/**
* Creates new form Scoreboard
*/
public Scoreboard() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panelQuestion = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
txtQuestion = new javax.swing.JTextPane();
panelQInfo = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
txtCategory = new javax.swing.JTextPane();
jScrollPane3 = new javax.swing.JScrollPane();
txtValue = new javax.swing.JTextPane();
panelScores = new javax.swing.JPanel();
panelTeamOne = new javax.swing.JPanel();
lblTeamOne = new javax.swing.JLabel();
lblTeamOneScore = new javax.swing.JLabel();
panelTeamTwo = new javax.swing.JPanel();
lblTeamOne1 = new javax.swing.JLabel();
lblTeamTwoScore = new javax.swing.JLabel();
panelTeamThree = new javax.swing.JPanel();
lblTeamThree = new javax.swing.JLabel();
lblTeamThreeScore = new javax.swing.JLabel();
btnAnswer = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
txtSpins = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("GameGUI");
setName("frame_Scoreboard"); // NOI18N
setPreferredSize(new java.awt.Dimension(1000, 1000));
setResizable(false);
jScrollPane1.setViewportView(txtQuestion);
txtCategory.setMaximumSize(new java.awt.Dimension(6, 22));
jScrollPane2.setViewportView(txtCategory);
txtValue.setMaximumSize(new java.awt.Dimension(6, 22));
jScrollPane3.setViewportView(txtValue);
javax.swing.GroupLayout panelQInfoLayout = new javax.swing.GroupLayout(panelQInfo);
panelQInfo.setLayout(panelQInfoLayout);
panelQInfoLayout.setHorizontalGroup(
panelQInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelQInfoLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
.addContainerGap())
);
panelQInfoLayout.setVerticalGroup(
panelQInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelQInfoLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelQInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
.addComponent(jScrollPane2))
.addContainerGap())
);
javax.swing.GroupLayout panelQuestionLayout = new javax.swing.GroupLayout(panelQuestion);
panelQuestion.setLayout(panelQuestionLayout);
panelQuestionLayout.setHorizontalGroup(
panelQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelQuestionLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(panelQInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
panelQuestionLayout.setVerticalGroup(
panelQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelQuestionLayout.createSequentialGroup()
.addContainerGap()
.addComponent(panelQInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE))
);
panelTeamOne.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
lblTeamOne.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
lblTeamOne.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTeamOne.setText("Team One Score:");
lblTeamOne.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
lblTeamOneScore.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblTeamOneScore.setForeground(new java.awt.Color(0, 153, 51));
lblTeamOneScore.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTeamOneScore.setText("750");
lblTeamOneScore.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
javax.swing.GroupLayout panelTeamOneLayout = new javax.swing.GroupLayout(panelTeamOne);
panelTeamOne.setLayout(panelTeamOneLayout);
panelTeamOneLayout.setHorizontalGroup(
panelTeamOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelTeamOneLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelTeamOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblTeamOneScore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTeamOneLayout.createSequentialGroup()
.addGap(0, 12, Short.MAX_VALUE)
.addComponent(lblTeamOne, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(9, 9, 9)))
.addContainerGap())
);
panelTeamOneLayout.setVerticalGroup(
panelTeamOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelTeamOneLayout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(lblTeamOne, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblTeamOneScore, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 14, Short.MAX_VALUE))
);
panelTeamTwo.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
lblTeamOne1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
lblTeamOne1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTeamOne1.setText("Team Two Score:");
lblTeamOne1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
lblTeamTwoScore.setBackground(new java.awt.Color(255, 204, 204));
lblTeamTwoScore.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblTeamTwoScore.setForeground(new java.awt.Color(204, 51, 0));
lblTeamTwoScore.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTeamTwoScore.setText("-200");
lblTeamTwoScore.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
javax.swing.GroupLayout panelTeamTwoLayout = new javax.swing.GroupLayout(panelTeamTwo);
panelTeamTwo.setLayout(panelTeamTwoLayout);
panelTeamTwoLayout.setHorizontalGroup(
panelTeamTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelTeamTwoLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelTeamTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblTeamTwoScore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTeamTwoLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(lblTeamOne1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(9, 9, 9)))
.addContainerGap())
);
panelTeamTwoLayout.setVerticalGroup(
panelTeamTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelTeamTwoLayout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(lblTeamOne1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblTeamTwoScore, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 15, Short.MAX_VALUE))
);
panelTeamThree.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
lblTeamThree.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
lblTeamThree.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTeamThree.setText("Team Three Score:");
lblTeamThree.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
lblTeamThreeScore.setBackground(new java.awt.Color(255, 204, 204));
lblTeamThreeScore.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblTeamThreeScore.setForeground(new java.awt.Color(0, 153, 51));
lblTeamThreeScore.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTeamThreeScore.setText("1300");
lblTeamThreeScore.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
javax.swing.GroupLayout panelTeamThreeLayout = new javax.swing.GroupLayout(panelTeamThree);
panelTeamThree.setLayout(panelTeamThreeLayout);
panelTeamThreeLayout.setHorizontalGroup(
panelTeamThreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelTeamThreeLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelTeamThreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblTeamThreeScore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTeamThreeLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(lblTeamThree, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(9, 9, 9)))
.addContainerGap())
);
panelTeamThreeLayout.setVerticalGroup(
panelTeamThreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelTeamThreeLayout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(lblTeamThree, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblTeamThreeScore, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 15, Short.MAX_VALUE))
);
btnAnswer.setText("Show Answer");
txtSpins.setText("There are <x> spins remaining.");
jScrollPane4.setViewportView(txtSpins);
javax.swing.GroupLayout panelScoresLayout = new javax.swing.GroupLayout(panelScores);
panelScores.setLayout(panelScoresLayout);
panelScoresLayout.setHorizontalGroup(
panelScoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelScoresLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelScoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane4)
.addGroup(panelScoresLayout.createSequentialGroup()
.addGroup(panelScoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(panelTeamOne, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelTeamTwo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelTeamThree, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
.addComponent(btnAnswer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
panelScoresLayout.setVerticalGroup(
panelScoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelScoresLayout.createSequentialGroup()
.addComponent(panelTeamOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(panelTeamTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(panelTeamThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jScrollPane4)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panelQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panelScores, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(23, 23, 23))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panelQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(panelScores, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Scoreboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Scoreboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Scoreboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Scoreboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Scoreboard().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAnswer;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JLabel lblTeamOne;
private javax.swing.JLabel lblTeamOne1;
private javax.swing.JLabel lblTeamOneScore;
private javax.swing.JLabel lblTeamThree;
private javax.swing.JLabel lblTeamThreeScore;
private javax.swing.JLabel lblTeamTwoScore;
private javax.swing.JPanel panelQInfo;
private javax.swing.JPanel panelQuestion;
private javax.swing.JPanel panelScores;
private javax.swing.JPanel panelTeamOne;
private javax.swing.JPanel panelTeamThree;
private javax.swing.JPanel panelTeamTwo;
private javax.swing.JTextPane txtCategory;
private javax.swing.JTextPane txtQuestion;
private javax.swing.JTextPane txtSpins;
private javax.swing.JTextPane txtValue;
// End of variables declaration
}
谢谢!
答案 0 :(得分:1)
setPreferredSize(new java.awt.Dimension(1000, 1000));
是问题所在。您正在将大小明确更改为1000x1000
。您需要删除该行并将其替换为pack();
。 pack()
将使JFrame
能够很好地容纳它所容纳的所有组件。
答案 1 :(得分:-1)
我找到了解决方法。