如何在Java中创建带圆边的框架..我已经创建了圆边按钮

时间:2010-11-26 17:22:54

标签: java swing

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.border.*;

public class ChangeButtonShape {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
JLabel l=new JLabel("Name");
final JTextField text=new JTextField(20);
JButton button = new JButton("Go");
//button.setBackground(Color.lightGray);
l.setBounds(10,10,100,20);
text.setBounds(100,10,180,20);
button.setBounds(10,40,50,20);
button.setBorder(new RoundedBorder(10));
frame.add(l);
frame.add(text);
frame.add(button);

button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
String st=text.getText();
JOptionPane.showMessageDialog(null,"Hello "+st);
    }
});
frame.setSize(300,150);
frame.setVisible(true);
}
}
class RoundedBorder implements Border {
        int radius;
        RoundedBorder(int radius) {
            this.radius = radius;
        }
        public Insets getBorderInsets(Component c) {
            return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
        }
        public boolean isBorderOpaque() {
            return true;
        }
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            g.drawRoundRect(x,y,width-1,height-1,radius,radius);
        }
    }

1 个答案:

答案 0 :(得分:2)

我认为可以使用AWTUtilities.setWindowShape。看一下这篇文章:http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html

但是,AWTUtilities尚未公开,因为它位于com.sun.awt包中,因此您不应该使用它。我相信它将在Java 7中发布。