Java Swing如何在Nimbus中设置JButton大小?
我试图使我的按钮在GUI中具有相同的大小。然而,每当我设置PreferredSize时,Nimbus似乎会自动设置一些按钮的高度。让Nimbus使所有按钮具有相同大小(即宽度和高度)的最佳方法是什么?在下面的示例代码中,第一行按钮的高度正确,但宽度不正确。在第二行中,宽度正确,但高度不正确。如何使所有按钮在Nimbus中显示相同的大小Java Swing如何在Nimbus中设置JButton大小?,java,swing,jbutton,nimbus,Java,Swing,Jbutton,Nimbus,我试图使我的按钮在GUI中具有相同的大小。然而,每当我设置PreferredSize时,Nimbus似乎会自动设置一些按钮的高度。让Nimbus使所有按钮具有相同大小(即宽度和高度)的最佳方法是什么?在下面的示例代码中,第一行按钮的高度正确,但宽度不正确。在第二行中,宽度正确,但高度不正确。如何使所有按钮在Nimbus中显示相同的大小 public class SampleNimbusProblem extends javax.swing.JDialog { /** Creates n
public class SampleNimbusProblem extends javax.swing.JDialog {
/** Creates new form SampleNimbusProblem */
public SampleNimbusProblem(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
okButton1 = new javax.swing.JButton();
cacelButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
jPanel1.setLayout(new java.awt.GridBagLayout());
okButton.setText("OK");
okButton.setMaximumSize(new java.awt.Dimension(65, 23));
okButton.setMinimumSize(new java.awt.Dimension(65, 23));
okButton.setPreferredSize(new java.awt.Dimension(65, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel1.add(okButton, gridBagConstraints);
cancelButton.setText("Cancel");
cancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
cancelButton.setMinimumSize(new java.awt.Dimension(65, 23));
cancelButton.setPreferredSize(new java.awt.Dimension(65, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel1.add(cancelButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
getContentPane().add(jPanel1, gridBagConstraints);
jPanel2.setLayout(new java.awt.GridBagLayout());
okButton1.setText("OK");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel2.add(okButton1, gridBagConstraints);
cacelButton2.setText("Cancel");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel2.add(cacelButton2, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
getContentPane().add(jPanel2, gridBagConstraints);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
SampleNimbusProblem dialog = new SampleNimbusProblem(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cacelButton2;
private javax.swing.JButton cancelButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JButton okButton;
private javax.swing.JButton okButton1;
// End of variables declaration
}
标准化尺寸的一种方法是将组件放入一个容器中。有关更多详细信息,请参阅 …无论何时我设置了首选尺寸
顺便说一句,很少有必要设置首选尺寸。让具有适当填充和边框的布局管理器在不同的屏幕分辨率、大小、PLAF和版本中计算组件的正确大小。标准化大小的一种方法是将组件放入一个文件夹中。有关更多详细信息,请参阅 …无论何时我设置了首选尺寸
顺便说一句,很少有必要设置首选尺寸。让具有适当填充和边框的布局管理器在不同的屏幕分辨率、大小、PLAF和版本中计算组件的正确大小。没错,相当多的错误开发在第一季度某处结束,在某些情况下忽略了PreferredSize的设置,这就是为什么会有你是对的,相当多的错误开发在第一季度的某个地方结束了,在某些情况下忽略了PreferredSize的设置,这就是为什么会有这里是我希望示例出现的方式。使用GroupLayout成功了。它正确地出现在Nimbus LAF中。再次感谢你的帮助。我想我需要重新考虑一下我对GridBagLayout的偏好
public class SampleNimbusProblem extends javax.swing.JDialog {
/** Creates new form SampleNimbusProblem */
public SampleNimbusProblem(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
okButton1 = new javax.swing.JButton();
cacelButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
okButton.setText("OK");
okButton.setMaximumSize(new java.awt.Dimension(65, 23));
okButton.setMinimumSize(new java.awt.Dimension(65, 23));
okButton.setPreferredSize(new java.awt.Dimension(65, 23));
cancelButton.setText("Cancel");
cancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
cancelButton.setMinimumSize(new java.awt.Dimension(65, 23));
cancelButton.setPreferredSize(new java.awt.Dimension(65, 23));
okButton1.setText("OK");
cacelButton2.setText("Cancel");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(5, 5, 5)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(okButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cacelButton2))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(5, 5, 5)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cacelButton2)
.addComponent(okButton1))
.addContainerGap(102, Short.MAX_VALUE))
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(10, 125, 112, 125);
getContentPane().add(jPanel1, gridBagConstraints);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
SampleNimbusProblem dialog = new SampleNimbusProblem(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cacelButton2;
private javax.swing.JButton cancelButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton okButton;
private javax.swing.JButton okButton1;
// End of variables declaration
}以下是我希望示例的显示方式。使用GroupLayout成功了。它正确地出现在Nimbus LAF中。再次感谢你的帮助。我想我需要重新考虑一下我对GridBagLayout的偏好
public class SampleNimbusProblem extends javax.swing.JDialog {
/** Creates new form SampleNimbusProblem */
public SampleNimbusProblem(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
okButton1 = new javax.swing.JButton();
cacelButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
okButton.setText("OK");
okButton.setMaximumSize(new java.awt.Dimension(65, 23));
okButton.setMinimumSize(new java.awt.Dimension(65, 23));
okButton.setPreferredSize(new java.awt.Dimension(65, 23));
cancelButton.setText("Cancel");
cancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
cancelButton.setMinimumSize(new java.awt.Dimension(65, 23));
cancelButton.setPreferredSize(new java.awt.Dimension(65, 23));
okButton1.setText("OK");
cacelButton2.setText("Cancel");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(5, 5, 5)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(okButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cacelButton2))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(5, 5, 5)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cacelButton2)
.addComponent(okButton1))
.addContainerGap(102, Short.MAX_VALUE))
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(10, 125, 112, 125);
getContentPane().add(jPanel1, gridBagConstraints);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
SampleNimbusProblem dialog = new SampleNimbusProblem(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cacelButton2;
private javax.swing.JButton cancelButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton okButton;
private javax.swing.JButton okButton1;
// End of variables declaration
}谢谢。虽然GridLayout没有给我太多的布局控制权,但唯一的解决方案似乎是使用不同的LayoutManager。GridBagConstraints似乎不能很好地与Nimbus中的JButtons配合使用。我能够使用GroupLayout获得正确的布局。GridLayout将使按钮大小均匀,这是一种简单的方法。如果您需要在GUI的不同部分使用不同的效果,那么可以使用一个。但是你是说你已经有一个GroupLayout的工作布局了吗?是的,我现在使用的是嵌套布局。有多个面板,每个面板都有自己的布局。我对每个面板都使用GridBagConstraint,但在Nimbus中无法使布局正常工作。我的JDialog中所有面板的主布局管理器是GridbagConstraints。然而,我的按钮面板现在使用的是GroupLayout,它似乎工作得很好。谢谢你的建议!很高兴你把它整理好了你应该举一个例子作为答案,并将其标记为正确。谢谢。虽然GridLayout没有给我太多的布局控制权,但唯一的解决方案似乎是使用不同的LayoutManager。GridBagConstraints似乎不能很好地与Nimbus中的JButtons配合使用。我能够使用GroupLayout获得正确的布局。GridLayout将使按钮大小均匀,这是一种简单的方法。如果您需要在GUI的不同部分使用不同的效果,那么可以使用一个。但是你是说你已经有一个GroupLayout的工作布局了吗?是的,我现在使用的是嵌套布局。有多个面板,每个面板都有自己的布局。我对每个面板都使用GridBagConstraint,但在Nimbus中无法使布局正常工作。我的JDialog中所有面板的主布局管理器是GridbagConstraints。然而,我的按钮面板现在使用的是GroupLayout,它似乎工作得很好。谢谢你的建议!很高兴你把它整理好了你应该举一个例子作为答案,并把它标记为正确。