62,615
社区成员
发帖
与我相关
我的任务
分享
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 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 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);
}
}
}
class HelloWorldPanel extends JPanel impletments ActionListener{
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);
}
}
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 {
//*******************************
//???????????????????????????????
//*******************************
private static final int leftX = 100;
private static final int topY = 200;
}