Java 如何使用Swing在JFrame上应用JTatto主题
我试图在我的swing项目中应用JTatto外观和感觉 主题正在某些窗体上应用,但在其他窗体上,控件无法正常工作。它们相互重叠 要应用主题,我使用以下代码Java 如何使用Swing在JFrame上应用JTatto主题,java,swing,themes,look-and-feel,Java,Swing,Themes,Look And Feel,我试图在我的swing项目中应用JTatto外观和感觉 主题正在某些窗体上应用,但在其他窗体上,控件无法正常工作。它们相互重叠 要应用主题,我使用以下代码 try { String lookandfeel="com.jtattoo.plaf.smart.SmartLookAndFeel"; UIManager.setLookAndFeel(lookandfeel); } catch(Exception ex) { ex.printStackTrace(); } 工作如我所料 L
try
{
String lookandfeel="com.jtattoo.plaf.smart.SmartLookAndFeel";
UIManager.setLookAndFeel(lookandfeel);
}
catch(Exception ex)
{
ex.printStackTrace();
}
- 工作如我所料
- L&F必须是
- 在GUI创建之前初始化
- 对于运行时的更改,通过调用应用于all,其中更简单的是
==顶级容器的局部变量,否则您必须迭代并将此外观分别应用于当前组件树中的每个JComponents组件c
例如不适当地使用
null
布局和setBounds
。为了更快地获得更好的帮助,请发布一个。有些表格是单独的窗口或其他什么
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
public class MenuExample extends JPanel {
private static final long serialVersionUID = 1L;
private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
private Icon questIcon = UIManager.getIcon("OptionPane.questionIcon");
private JTextPane pane;
private JMenuBar menuBar;
public MenuExample() {
menuBar = new JMenuBar();
JMenu formatMenu = new JMenu("Justify");
formatMenu.setMnemonic('J');
MenuAction leftJustifyAction = new MenuAction("Left", errorIcon);
MenuAction rightJustifyAction = new MenuAction("Right", infoIcon);
MenuAction centerJustifyAction = new MenuAction("Center", warnIcon);
MenuAction fullJustifyAction = new MenuAction("Full", questIcon);
JMenuItem item;
item = formatMenu.add(leftJustifyAction);
item.setMnemonic('L');
item = formatMenu.add(rightJustifyAction);
item.setMnemonic('R');
item = formatMenu.add(centerJustifyAction);
item.setMnemonic('C');
item = formatMenu.add(fullJustifyAction);
item.setMnemonic('F');
menuBar.add(formatMenu);
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
pane = new JTextPane();
pane.setPreferredSize(new Dimension(250, 250));
pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
JFrame frame = new JFrame("Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menuBar);
frame.add(pane, BorderLayout.CENTER);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
class MenuAction extends AbstractAction {
public MenuAction(String text, Icon icon) {
super(text, icon);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
pane.getStyledDocument().insertString(0,
"Action [" + e.getActionCommand()
+ "] performed!\n", null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String s[]) {
try {
UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MenuExample example = new MenuExample();
}
});
}
}