通过改写 原始对象的paint 方法,来设置对象的border颜色
解决方案:
//通过改写 原始对象的paint 方法,来设置对象的border颜色
1.
package test1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class MyFrame {
public static void main(String[] args) {
JFrame frame1 = new JFrame();
frame1.setBounds(400, 300, 200, 200);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.red));
frame1.add(panel);
frame1.setVisible(true);
JFrame frame2 = new JFrame() {
// public void paint(Graphics g) {
// super.paint(g);
// Rectangle rect = this.getBounds();
// int width = (int) rect.getWidth() - 1;
// int height = (int) rect.getHeight() - 1;
// g.setColor(Color.red);
// g.drawRect(0, 0, width, height);
// }
//通过改写 原始对象的paint 方法,来设置对象的border颜色
public void paint(Graphics g) {
super.paint(g);
Rectangle rect = this.getBounds();
int width = (int) rect.getWidth() - 1;
int height = (int) rect.getHeight() - 1;
g.setColor(Color.red);
g.drawRect(0, 0, width, height);
}
};
frame2.setBounds(650, 300, 200, 200);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setUndecorated(true);//隐藏标题栏 按钮
frame2.setVisible(true);
}
}
2.
package test1;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.FileWriter;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
public class Pic {
private JFrame frmDrag;
private JTextField textField;
private final ButtonGroup buttonGroup = new ButtonGroup();
String str1 = null, str2 = null;
// private JTextField textField_1;
// private final Action action = new SwingAction();
private Font font1 =new Font("Microsoft YaHei", Font.CENTER_BASELINE, 12);
private Font font2 =new Font("Microsoft YaHei", Font.BOLD, 16);
static FileWriter fw ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pic window = new Pic();
window.frmDrag.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Pic() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmDrag = new JFrame();
// frmDrag.setResizable(false);
frmDrag.setResizable(true);
// frmDrag.getContentPane().setBackground(new Color(0, 255, 0));
frmDrag.getContentPane().setBackground(Color.GREEN);
//windows green background
//? error 被panel 设置的颜色覆盖
// frmDrag.setBackground(new Color(0, 0, 255));
// frmDrag.setForeground(Color.BLUE);
frmDrag.setTitle("拼图游戏:Made by Xgqfrms");
frmDrag.setFont(font1);
frmDrag.setBounds(10, 10, 800, 600);
frmDrag.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmDrag.getContentPane().setLayout(null);
JPanel panel1 = new JPanel();
// panel1.setBackground(new Color(125, 125, 125));
panel1.setBackground(Color.PINK);
panel1.setBounds(0, 0, 600, 75);
frmDrag.getContentPane().add(panel1);
panel1.setLayout(null);
JLabel label_0 = new JLabel("按钮区");
label_0.setHorizontalAlignment(SwingConstants.CENTER);
label_0.setFont(font2);
label_0.setBounds(0, 0, 50, 30);
label_0.setForeground(Color.BLACK);
panel1.add(label_0);
// JLabel label = new JLabel("\u59D3\u540D:");
JLabel label = new JLabel("数字提示");
label.setHorizontalAlignment(SwingConstants.CENTER);
// label.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
label.setFont(font2);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(41, 35, 80, 30);//坐标(x,y,width,length)
label.setForeground(Color.BLACK);
panel1.add(label);
JLabel label_1 = new JLabel("清除提示");
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setFont(font2);
label_1.setBounds(141, 35, 80, 30);
label_1.setForeground(Color.BLACK);
panel1.add(label_1);
JLabel label_2 = new JLabel("选择图片:");
label_2.setHorizontalAlignment(SwingConstants.CENTER);
label_2.setFont(font2);
label_2.setBounds(250, 35, 80, 30);
label_2.setForeground(Color.BLACK);
panel1.add(label_2);
JRadioButton radioButton = new JRadioButton("");
buttonGroup.add(radioButton);// 按钮组
radioButton.setSelected(true);
// radioButton.setSelected(false);
radioButton.setBounds(130, 38, 20, 20);
radioButton.setBackground(Color.PINK);
panel1.add(radioButton);
JRadioButton radioButton_1 = new JRadioButton("");
buttonGroup.add(radioButton_1);
radioButton_1.setSelected(false);
radioButton_1.setBounds(30, 38, 20, 20);
radioButton_1.setBackground(Color.PINK);
panel1.add(radioButton_1);
textField = new JTextField();
textField.setBounds(330, 38, 100, 30);
textField.setForeground(Color.red);
textField.setBackground(Color.green);
panel1.add(textField);
textField.setColumns(8);
//通过改写 原始对象的paint 方法,来设置对象的border颜色
JButton button = new JButton("Start"){
public void paint(Graphics g) {
super.paint(g);
Rectangle rect = this.getBounds();
int width = (int) rect.getWidth() - 1;
int height = (int) rect.getHeight() - 1;
g.setColor(Color.red);
g.drawRect(0, 0, width, height);
}
};
// button.setAction(action);
button.setBounds(435, 38, 70, 30);
button.setBorderPainted(true);//没什么效果呀
// button.setName("Start");
button.setFont(font1);
// Border border1;
// button.setBorder(border1.paintBorder(c, g, 0, 0, 70, 30));
// void javax.swing.border.Border.paintBorder(Component c, Graphics g, int x, int y, int width, int height)
panel1.add(button);
Cursor cursor = new Cursor(12);// 光标样式
}
}
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/4876102.html
未经授权禁止转载,违者必究!