当前位置: 技术问答>java相关
怎样设置JButton的大小,形状(只是要它凸起),位置,颜色?
来源: 互联网 发布时间:2015-09-24
本文导语: 我是大菜鸟,不知做按钮: 我的源码: import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class TestNetPaint { public static int count=100; publi...
我是大菜鸟,不知做按钮:
我的源码:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class TestNetPaint
{
public static int count=100;
public static void main(String[] args)throws Exception
{
JFrame myframe=new MyFrame();
myframe.show();
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(600,400);
Container contentPane=getContentPane();
MyPanel testPanel=new MyPanel();
JButton myButton=new JButton("test");
testPanel.add(myButton);
contentPane.add(testPanel);
}
}
class MyPanel extends JPanel
{
TestNetPaint netpaint=new TestNetPaint();
public MyPanel()
{
}
public void paint(Graphics g)
{
super.paint(g);
System.out.println("MyPanel is running");
}
}
我只是要画个按钮,按钮出来了,很难看:是平的,按钮上的test外还有个不知所云的框。请问:怎样设置JButton的大小,形状(只是要它凸起),位置,颜色?最好给点源码,如能带按钮事件的处理代码就更好了~~~:),先谢了!
我的源码:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class TestNetPaint
{
public static int count=100;
public static void main(String[] args)throws Exception
{
JFrame myframe=new MyFrame();
myframe.show();
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(600,400);
Container contentPane=getContentPane();
MyPanel testPanel=new MyPanel();
JButton myButton=new JButton("test");
testPanel.add(myButton);
contentPane.add(testPanel);
}
}
class MyPanel extends JPanel
{
TestNetPaint netpaint=new TestNetPaint();
public MyPanel()
{
}
public void paint(Graphics g)
{
super.paint(g);
System.out.println("MyPanel is running");
}
}
我只是要画个按钮,按钮出来了,很难看:是平的,按钮上的test外还有个不知所云的框。请问:怎样设置JButton的大小,形状(只是要它凸起),位置,颜色?最好给点源码,如能带按钮事件的处理代码就更好了~~~:),先谢了!
|
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;
public class TestNetPaint
{
public static int count=100;
public static void main(String[] args)throws Exception
{
JFrame myframe=new MyFrame();
myframe.show();
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(600,400);
Container contentPane=getContentPane();
MyPanel testPanel=new MyPanel();
JButton myButton=new JButton("test");
myButton.setFocusPainted(false);//取消聚焦框
myButton.setForeground(Color.pink);
myButton.setBackground(Color.blue);
testPanel.add(myButton);
contentPane.add(testPanel);
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
System.exit(1);
}
}
}
class MyPanel extends JPanel
{
TestNetPaint netpaint=new TestNetPaint();
public MyPanel()
{
}
public void paint(Graphics g)
{
super.paint(g);
System.out.println("MyPanel is running");
}
}
/*说明:1,在Swing里面我感觉控件的大小,位置无所谓的,只要你布局做的对,原始窗口
大小合适就好。
2,myButton.setFocusPainted(false);//取消 按钮上的test外还有个不知所云的框
3,UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this); 这句不能少,否则没用
4, myButton.setForeground(Color.pink);
myButton.setBackground(Color.blue); 改变颜色
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;
public class TestNetPaint
{
public static int count=100;
public static void main(String[] args)throws Exception
{
JFrame myframe=new MyFrame();
myframe.show();
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(600,400);
Container contentPane=getContentPane();
MyPanel testPanel=new MyPanel();
JButton myButton=new JButton("test");
myButton.setFocusPainted(false);//取消聚焦框
myButton.setForeground(Color.pink);
myButton.setBackground(Color.blue);
testPanel.add(myButton);
contentPane.add(testPanel);
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
System.exit(1);
}
}
}
class MyPanel extends JPanel
{
TestNetPaint netpaint=new TestNetPaint();
public MyPanel()
{
}
public void paint(Graphics g)
{
super.paint(g);
System.out.println("MyPanel is running");
}
}
/*说明:1,在Swing里面我感觉控件的大小,位置无所谓的,只要你布局做的对,原始窗口
大小合适就好。
2,myButton.setFocusPainted(false);//取消 按钮上的test外还有个不知所云的框
3,UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this); 这句不能少,否则没用
4, myButton.setForeground(Color.pink);
myButton.setBackground(Color.blue); 改变颜色
*/
|
import javax.swing.UIManager;
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
System.exit(1);
}
这样你的按钮就会凸起来
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
System.exit(1);
}
这样你的按钮就会凸起来
|
大小 setSize()
位置 setLocation()
颜色 setBackground()和setForeground()
位置 setLocation()
颜色 setBackground()和setForeground()