我有一个简单的GUI,其中包含JButton,JMenuBar。这在正常情况下工作正常(显示和对齐)但如果我在UIManager代码下使用它不合适,
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
我面临的问题的SSSCE发布在下面,
import java.awt.HeadlessException;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class UiManagerEx extends JFrame{
JMenuBar menuBar;
JTextField textField;
JLabel labl;
JButton btn;
public static void main(String[] args) {
new UiManagerEx();
}
public UiManagerEx() throws HeadlessException {
init();
}
private void init(){
JPanel panel = new JPanel();
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Example");
menuBar.add(fileMenu);
JMenuItem menuAction = new JMenuItem("ONE");
fileMenu.add(menuAction);
JMenuItem menu1Action = new JMenuItem("TWO");
fileMenu.add(menu1Action);
labl = new JLabel("Test");
textField = new JTextField("example Program");
btn = new JButton("enable");
btn.setEnabled(false); // Make it true and the text will be visible.
GroupLayout settingsLayout = new GroupLayout(panel);
panel.setLayout(settingsLayout);
settingsLayout.setAutoCreateGaps(true);
settingsLayout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup = settingsLayout.createSequentialGroup();
hGroup.addGroup(settingsLayout.createParallelGroup()
.addComponent(labl));
hGroup.addGap(10);
hGroup.addGroup(settingsLayout.createParallelGroup()
.addComponent(textField,GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE));
hGroup.addGap(10);
hGroup.addGroup(settingsLayout.createParallelGroup()
.addComponent(btn,GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE));
hGroup.addGap(10);
settingsLayout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = settingsLayout.createSequentialGroup();
vGroup.addGap(10);
vGroup.addGroup(settingsLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(labl)
.addComponent(textField)
.addComponent(btn));
vGroup.addGap(10);
settingsLayout.setVerticalGroup(vGroup);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //Comment this to make "alignement of JmenuItem" work properly
} catch (ClassNotFoundException ex) {}
catch (InstantiationException ex) {}
catch (IllegalAccessException ex) {}
catch (UnsupportedLookAndFeelException ex) {}
add(panel);
setJMenuBar(menuBar);
setTitle("Stack overflow");
setResizable(true);
setSize(400, 200);
setLocationRelativeTo(this);
setVisible(true);
}
}
以原生外观,
Jbutton.SetEnabled(false)
在处理这些问题的java应用程序中是否有任何使用本机外观的感觉。
我是新手......请帮助!
答案 0 :(得分:0)
在创建对象之前设置外观。然后显示按钮文本: 但我无法解释原因。也许有一些初始化取决于LookAndFeel
private void init(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // Comment this to make
// "alignement of JmenuItem" work
// properly
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
catch (InstantiationException ex) {
ex.printStackTrace();
}
catch (IllegalAccessException ex) {
ex.printStackTrace();
}
catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel panel = new JPanel();