以前弄得一个小程序,不过这里面的界面样式似乎也不漂亮!
不知道你上面的图形到底用的哪一个?
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class LookAndFeelTest extends JFrame
{
public int a = 0;
public int b = 0;
public LookAndFeelTest()
{
super("多种窗口显示事例");
Container con = getContentPane();
JPanel jp = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JMenuBar jbar = new JMenuBar();
JMenu jmenu = new JMenu("窗口风格");
JMenuItem jitem1 = new JMenuItem("系统平台风格");
JMenuItem jitem2 = new JMenuItem("Motif风格");
JMenuItem jitem3 = new JMenuItem("跨平台风格");
jitem1.addActionListener(new AL());
jitem2.addActionListener(new AL());
jitem3.addActionListener(new AL());
jmenu.add(jitem1);
jmenu.add(jitem2);
jmenu.add(jitem3);
jbar.add(jmenu);
setJMenuBar(jbar);
final JLabel jla = new JLabel("输入文本:");
final JTextField jtext = new JTextField("文本框LookAndFeel测试");
jp1.add(jla);
jp1.add(jtext);
final JCheckBox jc1 = new JCheckBox("粗体");
final JCheckBox jc2 = new JCheckBox("斜体");
final JCheckBox jc3 = new JCheckBox("下划线");
jc1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jc1.isSelected()) a = 1;
else
a = 0;
}
});
jc2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jc2.isSelected()) b = 2;
else
b = 0;
}
});
jc3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jc3.isSelected())
{
jla.setText("<html><u>"+jla.getText()+"</u></html>");
}
else
//jla.setText("<html>"+jla.getText()+"</html>");//JTextField不能这样来设置
jla.setText(jla.getText().substring(9, jla.getText().length()-11));
}
});
jp2.setLayout(new GridLayout(1,3));
jp2.add(jc1);
jp2.add(jc2);
jp2.add(jc3);
JButton jb1 = new JButton("改变字体");
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String str = jtext.getText();
Font font = new Font("宋体",a+b,18);
jtext.setFont(font);
jtext.setText(str);
}});
JButton jb2= new JButton("退出");
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}});
jp3.add(jb1);
jp3.add(jb2);
jp.setLayout(new BorderLayout());
jp.add(jp1,BorderLayout.NORTH);
jp.add(jp2,BorderLayout.CENTER);
jp.add(jp3,BorderLayout.SOUTH);
jp.setBorder(BorderFactory.createTitledBorder("组件样式"));
con.add(jp);
setBackground(Color.white);
setSize(300,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class AL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("系统平台风格"))
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception ec)
{
ec.printStackTrace();
}
}
else if (e.getActionCommand().equals("Motif风格"))
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch(Exception ec)
{
ec.printStackTrace();
}
}
else
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception ec)
{
ec.printStackTrace();
}
}
javax.swing.SwingUtilities.updateComponentTreeUI(LookAndFeelTest.this);
}
}
public static void main(String[] args)
{
new LookAndFeelTest();
}
}