java编程题~~

2024-11-08 01:50:50
推荐回答(3个)
回答(1):

(一题)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {
public static void main(String[] args) {
ButtomFrame frame = new ButtomFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class ButtomFrame
extends JFrame
implements ActionListener {
JButton button;
public ButtomFrame() {
button = new JButton("0");
JPanel panel = new JPanel();
button.addActionListener(this);
panel.add(button);
setSize(300, 200);
Container contentpane = getContentPane();
contentpane.add(panel);
}

public void actionPerformed(ActionEvent event) {
String s = button.getText();
int a = Integer.parseInt(s) + 1;
button.setText(a + "");
}
}

(二题)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {
public static void main(String[] args) {
ButtomFrame frame = new ButtomFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class ButtomFrame
extends JFrame
implements ActionListener {
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JPanel panel1;
public ButtomFrame() {
button1 = new JButton("红");
button2 = new JButton("白");
button3 = new JButton("黑");
button4 = new JButton("蓝");
JPanel panel = new JPanel();
panel1 = new JPanel();
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
panel.setLayout(new BorderLayout());
panel.add(button1, BorderLayout.NORTH);
panel.add(button2, BorderLayout.SOUTH);
panel.add(button3, BorderLayout.EAST);
panel.add(button4, BorderLayout.WEST);
panel.add(panel1,BorderLayout.CENTER);
setSize(300, 200);
Container contentpane = getContentPane();
contentpane.add(panel);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
panel1.setBackground(Color.RED);
}
if(e.getSource() == button2){
panel1.setBackground(Color.WHITE);
}
if(e.getSource() == button3){
panel1.setBackground(Color.BLACK);
}
if(e.getSource() == button4){
panel1.setBackground(Color.BLUE);
}
}
}

回答(2):

两个问题一并解决,粘贴到名为Application的java文件编译执行即可查看效果

import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;

public class Application {
boolean packFrame = false;

public Application() {
Frame frame = new Frame();
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}

new Application();
}
});
}
}

class Frame extends JFrame {
JPanel contentPane;
BorderLayout layout = new BorderLayout();
JButton btnLeft = new JButton();
JButton btnTop = new JButton();
JButton btnDown = new JButton();
JButton btnRight = new JButton();
JPanel jpnMain = new JPanel();

public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(layout);
setSize(new Dimension(400, 300));
setTitle("应用程序按钮测试");
btnTop.setText("0");
btnDown.setText("0");
btnLeft.setText("0");
btnRight.setText("0");
btnTop.addActionListener(new Frame_btn_actionAdapter(this));
btnDown.addActionListener(new Frame_btn_actionAdapter(this));
btnLeft.addActionListener(new Frame_btn_actionAdapter(this));
btnRight.addActionListener(new Frame_btn_actionAdapter(this));
contentPane.add(btnLeft, java.awt.BorderLayout.WEST);
contentPane.add(btnDown, java.awt.BorderLayout.SOUTH);
contentPane.add(btnTop, java.awt.BorderLayout.NORTH);
contentPane.add(btnRight, java.awt.BorderLayout.EAST);
contentPane.add(jpnMain, java.awt.BorderLayout.CENTER);
}

public void btn_actionPerformed(ActionEvent e) {
((JButton) e.getSource()).setText(String.valueOf((new Integer(((JButton) e.getSource()).getText())).intValue() + 1));
if ((JButton) e.getSource() == this.btnTop) {
this.jpnMain.setBackground(Color.red);
} else if ((JButton) e.getSource() == this.btnDown) {
this.jpnMain.setBackground(Color.yellow);
} else if ((JButton) e.getSource() == this.btnLeft) {
this.jpnMain.setBackground(Color.blue);
} else if ((JButton) e.getSource() == this.btnRight) {
this.jpnMain.setBackground(Color.green);
}
}
}

class Frame_btn_actionAdapter implements ActionListener {
private Frame adaptee;

Frame_btn_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn_actionPerformed(e);
}
}

回答(3):

这是java刚开始学时界面编程的最基本的事件响应、窗口布局管理的应用,你应该自己写写看,这是最基本的,不要靠别人,你自己一定可以搞定。。。你如果编写出来,那种成功后的喜悦真是太棒了。。。。试试吧