题目:添加三个按钮Red, Yellow, Blue,使窗口中央的"Hello world"显示相应的颜色。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;public class HelloWorld {
public static void main(String[] args) {
HelloWorldFrame frame = new HelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class HelloWorldFrame extends JFrame {
public JFrame() {
add(new HelloWorldPanel());
setSize(WIDTH, HEIGHT);
} private static final int WIDTH = 400;
private static final int HEIGHT = 400;
}class HelloWorldPanel extends JPanel {
//*******************************
//??????????????????????????????? //*******************************
private static final int leftX = 100;
private static final int topY = 200;
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;public class HelloWorld {
public static void main(String[] args) {
HelloWorldFrame frame = new HelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class HelloWorldFrame extends JFrame {
public JFrame() {
add(new HelloWorldPanel());
setSize(WIDTH, HEIGHT);
} private static final int WIDTH = 400;
private static final int HEIGHT = 400;
}class HelloWorldPanel extends JPanel {
//*******************************
//??????????????????????????????? //*******************************
private static final int leftX = 100;
private static final int topY = 200;
}
解决方案 »
- robot中createScreenCapture 求救
- final类除了吥能被继承外,好像没什么了!
- Jfreechar 当鼠标滑到数据项时展示的文字
- 一段代码,请高手讲解
- 关于JAVA连接SQLServer2000的问题,为什么总是java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing
- 我下了个jexcelapi应该放在哪里?
- 菜单里如何使用CheckBox,并去掉它前面的方框啊
- extends JPanel无法设布局??
- 哪位给我一个线同步最简单的例子
- 怎样用java程序实现自动提交数据到目标网站。十分感谢
- 一个关于在Jtable上显示数据库信息的问题!!
- JtextArea问题
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;public class HelloWorld {
public static void main(String[] args) {
HelloWorldFrame frame = new HelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class HelloWorldFrame extends JFrame {
public HelloWorldFrame() {
add(new HelloWorldPanel());
setSize(WIDTH, HEIGHT);
} private static final int WIDTH = 400;
private static final int HEIGHT = 400;
}class HelloWorldPanel extends JPanel {
//*******************************
//??????????????????????????????? //*******************************
private static final int leftX = 100;
private static final int topY = 200;
}
JPanel p=new JPanel();
JButton r,b,y;
JLabel l;
l=new JLable("Hello world!");
r=new JButton("Red");
y=new JButton("Yellow");
b=new JButton("Blue");
p.add(r);
p.add(y);
p.add(b);
r.addActionListener(this);
b.addActionListener(this);
y.addActionListener(this);
getContentPane.add(l,BorderLayout.CENTER);
getContentPane.add(p,BorderLayout.SOUTH);
setSize(leftX,topY);
setVisiable(true);
private static final int leftX = 100;
private static final int topY = 200;
public void actionperformed(ActionEvent e){
if(e.getSource==r){
l.setbackground(Color.red);
}
if(e.getSource==b){
l.setbackground(Color.blue);
}
if(e.getSource==y){
l.setbackground(Color.yellow);
}
}
如果要让"Hello World"本身的字体颜色改变,应该怎么写?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;public class HelloWorld {
public static void main(String[] args) {
HelloWorldFrame frame = new HelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class HelloWorldFrame extends JFrame {
public HelloWorldFrame() {
add(new HelloWorldPanel());
setSize(WIDTH, HEIGHT);
} private static final int WIDTH = 400;
private static final int HEIGHT = 400;
}class HelloWorldPanel extends JPanel implements ActionListener
{
JPanel p=new JPanel();
JButton r,b,y;
JLabel l;
public HelloWorldPanel()
{
l=new JLabel("Hello world!");
r=new JButton("Red");
y=new JButton("Yellow");
b=new JButton("Blue");
r.addActionListener(this);
b.addActionListener(this);
y.addActionListener(this);
p.add(r);
p.add(y);
p.add(b);
add(l,BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
setSize(leftX,topY);
this.setVisible(true);
}
private static final int leftX = 100;
private static final int topY = 200; public void actionPerformed(java.awt.event.ActionEvent e)
{
if(e.getSource()==r){
l.setForeground(Color.red);
}
if(e.getSource()==b){
l.setForeground(Color.blue);
}
if(e.getSource()==y){
l.setForeground(Color.yellow);
}
}
}
import java.awt.event.*;
import javax.swing.*;public class ButtonTest {
public static void main(String[] args) {
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class ButtonFrame extends JFrame {
public ButtonFrame() {
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}class ButtonPanel extends JPanel {
public ButtonPanel() {
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener {
private Color backgroundColor;
public ColorAction(Color c) {
backgroundColor = c;
}
public void actionPerformed(ActionEvent event) {
setBackground(backgroundColor);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ButtonTest {
public static void main(String[] args) {
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class ButtonFrame extends JFrame {
public ButtonFrame() {
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}class ButtonPanel extends JPanel {
public ButtonPanel() {
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener {
private Color backgroundColor;
public ColorAction(Color c) {
backgroundColor = c;
}
public void actionPerformed(ActionEvent event) {
setBackground(backgroundColor);
}
}
}
简单写了一点。恩,你是说panel里面不要标签,直接写字吧?
你看一下代码,如果还不是你要的,那么描述的再详细一些。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class HelloWorld {
public static void main(String[] args) {
HelloWorldFrame frame = new HelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class HelloWorldFrame extends JFrame {
public HelloWorldFrame() {
add(new HelloWorldPanel());
setSize(WIDTH, HEIGHT);
} private static final int WIDTH = 400;
private static final int HEIGHT = 400;
}class HelloWorldPanel extends JPanel {
private Color color = Color.WHITE; public HelloWorldPanel() {
setLayout(new FlowLayout());
JButton redButton = new JButton("Red");
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
add(redButton);
add(yellowButton);
add(blueButton); redButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
color = Color.red;
repaint();
}
});
yellowButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
color = Color.YELLOW;
repaint();
}
});
blueButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
color = Color.BLUE;
repaint();
}
});
} @Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(color);
g.drawString("HelloWorld", leftX, topY);
} private static final int leftX = 100;
private static final int topY = 200;
}
Good!