gpt4 book ai didi

Java Swing 如何在 Nimbus 中设置 JButton 大小?

转载 作者:行者123 更新时间:2023-11-29 08:07:58 25 4
gpt4 key购买 nike

我试图让我的按钮在我的 GUI 中具有相同的大小。但是,每当我设置 PreferredSize() 时,Nimbus 似乎会自动设置某些按钮的高度。让 Nimbus 使所有按钮具有相同大小(即宽度和高度)的最佳方法是什么?在下面的示例代码中,第一行按钮的高度是正确的,但宽度不是。在第二行中,宽度正确但高度不正确。如何让所有按钮在 Nimbus 中显示相同的大小?

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

}

最佳答案

标准化大小的一种方法是将组件放入 GridLayout 中.参见 Laying Out Components Within a Container了解更多详情。


..whenever I setPreferredSize(),

顺便说一句 - 很少(如果有的话)需要设置首选大小。让布局管理器(使用适当的填充和边框)完成跨不同屏幕分辨率、尺寸、PLAF 和版本计算组件正确尺寸的繁重工作。

关于Java Swing 如何在 Nimbus 中设置 JButton 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9860784/

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