环境:myeclipse10 windowsXP jdk1.7 jre7问题:如上图,第一个按钮形状不正常,不应该是鼓起来的。这种现像仅是在WindowsLookAndFeel风格下才有。代码中我对每个JButton都执行了:
setFocusPainted(false);
setSelected(false);请问是什么原因?附全部代码:package jNote;public class Main {
public static void main(String[] args)
{
Window wnd = new Window();
wnd.setVisible(true);
}
}
package jNote;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Window extends JFrame{
public Window()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭动作
this.setSize(800, 600);// 设置窗口大小
this.setTitle("JNote"); //设置标题
this.setLocationRelativeTo(null);// 使窗口显示在屏幕中央
//this.setExtendedState(JFrame.MAXIMIZED_BOTH);//初始窗口最大化
try{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
catch(Exception e){}
ImageIcon imgIcon = new ImageIcon(this.getClass().getResource("images/jNote_16.png"));
Image img = imgIcon.getImage();
this.setIconImage(img);
JToolBar tb = new JToolBar();
tb.setPreferredSize(new Dimension(800,30));
tb.setFloatable(false);//是否可拖动
JButton bt_new = new JButton();
bt_new.setPreferredSize(new Dimension(20,20));
bt_new.setOpaque(false);
bt_new.setFocusPainted(false);
bt_new.setIcon(new ImageIcon(this.getClass().getResource("images/new.png")));
bt_new.setToolTipText("新建");
bt_new.setBorderPainted(false);
bt_new.setSelected(false);
//tb.addSeparator();
JButton bt_open = new JButton();
bt_open.setPreferredSize(new Dimension(20,20));
bt_open.setOpaque(false);
bt_open.setFocusPainted(false);
bt_open.setIcon(new ImageIcon(this.getClass().getResource("images/open.png")));
bt_open.setToolTipText("打开");
bt_open.setBorderPainted(false);
bt_open.setSelected(false);
JButton bt_save = new JButton();
bt_save.setPreferredSize(new Dimension(20,20));
bt_save.setOpaque(false);
bt_save.setFocusPainted(false);
bt_save.setIcon(new ImageIcon(this.getClass().getResource("images/save.png")));
bt_save.setToolTipText("保存");
bt_save.setBorderPainted(false);
bt_save.setSelected(false);
bt_new.addActionListener(new NewHandler());
tb.add(bt_new);
tb.add(bt_open);
tb.add(bt_save); this.getContentPane().add(tb,BorderLayout.NORTH);
//设置swing窗口系统风格
//String look = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//String look = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//String look = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
//String look = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
String look = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
try
{
UIManager.setLookAndFeel(look);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception ex)
{}
}
protected void processWindowEvent(WindowEvent e) {
boolean enable = false;
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
//处理Jframe关闭事件
int option= JOptionPane.showConfirmDialog(
this,"您确定要退出程序吗?","JNote"
,JOptionPane.YES_NO_OPTION); enable = (option==1);
} if(!enable){
//忽略其他事件,交给JFrame处理
super.processWindowEvent(e);
}
}
//事件处理
private class NewHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String buttonName = e.getActionCommand();
JOptionPane.showMessageDialog(null,buttonName + "被点击");
}
}
}
setFocusPainted(false);
setSelected(false);请问是什么原因?附全部代码:package jNote;public class Main {
public static void main(String[] args)
{
Window wnd = new Window();
wnd.setVisible(true);
}
}
package jNote;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Window extends JFrame{
public Window()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭动作
this.setSize(800, 600);// 设置窗口大小
this.setTitle("JNote"); //设置标题
this.setLocationRelativeTo(null);// 使窗口显示在屏幕中央
//this.setExtendedState(JFrame.MAXIMIZED_BOTH);//初始窗口最大化
try{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
catch(Exception e){}
ImageIcon imgIcon = new ImageIcon(this.getClass().getResource("images/jNote_16.png"));
Image img = imgIcon.getImage();
this.setIconImage(img);
JToolBar tb = new JToolBar();
tb.setPreferredSize(new Dimension(800,30));
tb.setFloatable(false);//是否可拖动
JButton bt_new = new JButton();
bt_new.setPreferredSize(new Dimension(20,20));
bt_new.setOpaque(false);
bt_new.setFocusPainted(false);
bt_new.setIcon(new ImageIcon(this.getClass().getResource("images/new.png")));
bt_new.setToolTipText("新建");
bt_new.setBorderPainted(false);
bt_new.setSelected(false);
//tb.addSeparator();
JButton bt_open = new JButton();
bt_open.setPreferredSize(new Dimension(20,20));
bt_open.setOpaque(false);
bt_open.setFocusPainted(false);
bt_open.setIcon(new ImageIcon(this.getClass().getResource("images/open.png")));
bt_open.setToolTipText("打开");
bt_open.setBorderPainted(false);
bt_open.setSelected(false);
JButton bt_save = new JButton();
bt_save.setPreferredSize(new Dimension(20,20));
bt_save.setOpaque(false);
bt_save.setFocusPainted(false);
bt_save.setIcon(new ImageIcon(this.getClass().getResource("images/save.png")));
bt_save.setToolTipText("保存");
bt_save.setBorderPainted(false);
bt_save.setSelected(false);
bt_new.addActionListener(new NewHandler());
tb.add(bt_new);
tb.add(bt_open);
tb.add(bt_save); this.getContentPane().add(tb,BorderLayout.NORTH);
//设置swing窗口系统风格
//String look = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//String look = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//String look = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
//String look = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
String look = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
try
{
UIManager.setLookAndFeel(look);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception ex)
{}
}
protected void processWindowEvent(WindowEvent e) {
boolean enable = false;
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
//处理Jframe关闭事件
int option= JOptionPane.showConfirmDialog(
this,"您确定要退出程序吗?","JNote"
,JOptionPane.YES_NO_OPTION); enable = (option==1);
} if(!enable){
//忽略其他事件,交给JFrame处理
super.processWindowEvent(e);
}
}
//事件处理
private class NewHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String buttonName = e.getActionCommand();
JOptionPane.showMessageDialog(null,buttonName + "被点击");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货