当前位置: 技术问答>java相关
java 基础:Swing中的按钮太难看了,怎样改变模式呢?
来源: 互联网 发布时间:2015-02-03
本文导语: 能把按钮变得象平常的一样吗? | 在java 中由三种界面样式 Metal是默认的样式 个平台运行一致 Windows 是在windows中的样式 Motif import javax.swing.*; import java.awt.*; import java.awt.event.*; class plafP...
能把按钮变得象平常的一样吗?
|
在java 中由三种界面样式
Metal是默认的样式 个平台运行一致
Windows 是在windows中的样式
Motif
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class plafPanel extends JPanel implements ActionListener
{
public plafPanel()
{
metalButton=new JButton("Metal");
motifButton=new JButton("Motif");
windowsButton=new JButton("Windows");
add(metalButton);
add(motifButton);
add(windowsButton);
metalButton.addActionListener(this);
motifButton.addActionListener(this);
windowsButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
String plaf="";
if (source==metalButton)
plaf="javax.swing.plaf.metal.MetalLookAndFell";
else if (source==motifButton)
plaf="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
else if (source==windowsButton)
plaf="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try
{
//diao yong UIManager.setLookAndFeel() method and SwingUtilities.updateComponetTree() methoe;
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){}
}
private JButton metalButton;
private JButton motifButton;
private JButton windowsButton;
}
class plafFrame extends JFrame
{
public plafFrame()
{
setTitle("plafTest");
setSize(300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new plafPanel());
}
}
public class PlafTest
{
public static void main(String args[])
{
JFrame test=new plafFrame();
test.setVisible(true);
}
}
Metal是默认的样式 个平台运行一致
Windows 是在windows中的样式
Motif
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class plafPanel extends JPanel implements ActionListener
{
public plafPanel()
{
metalButton=new JButton("Metal");
motifButton=new JButton("Motif");
windowsButton=new JButton("Windows");
add(metalButton);
add(motifButton);
add(windowsButton);
metalButton.addActionListener(this);
motifButton.addActionListener(this);
windowsButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
String plaf="";
if (source==metalButton)
plaf="javax.swing.plaf.metal.MetalLookAndFell";
else if (source==motifButton)
plaf="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
else if (source==windowsButton)
plaf="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try
{
//diao yong UIManager.setLookAndFeel() method and SwingUtilities.updateComponetTree() methoe;
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){}
}
private JButton metalButton;
private JButton motifButton;
private JButton windowsButton;
}
class plafFrame extends JFrame
{
public plafFrame()
{
setTitle("plafTest");
setSize(300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new plafPanel());
}
}
public class PlafTest
{
public static void main(String args[])
{
JFrame test=new plafFrame();
test.setVisible(true);
}
}