//ButtonClick.javaimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ButtonClick
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonClick");
setSize(WIDTH, HEIGHT);
// 将panel加入到frame
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}class ButtonPanel extends JPanel
{
public ButtonPanel()
{
//创建按钮
JButton Button1 = new JButton("按钮一");
JButton Button2 = new JButton("按钮二");
//将按钮加入panel
add(Button1);
add(Button2);
//创建监听器
ButtonAction ButtonAction1 = new ButtonAction("按钮一");
ButtonAction ButtonAction2 = new ButtonAction("按钮二");
//注册监听器
Button1.addActionListener(ButtonAction1);
Button2.addActionListener(ButtonAction2);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); //显示用户的点击操作
g.drawString(" " + ButtonText, 90, 120);
System.out.println();
}
private class ButtonAction
implements ActionListener
{
public ButtonAction(String text)
{
clicktext =text;
}
public void actionPerformed(ActionEvent event)
{
ButtonText += ("您点击的是:"+clicktext );
repaint();
}
private String clicktext="";
} public String ButtonText = "";
}哪位兄弟能帮我改一下程序,让每次点一下结果会换行,而不是一直往后面添加
import java.awt.event.*;
import javax.swing.*;public class ButtonClick
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonClick");
setSize(WIDTH, HEIGHT);
// 将panel加入到frame
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}class ButtonPanel extends JPanel
{
public ButtonPanel()
{
//创建按钮
JButton Button1 = new JButton("按钮一");
JButton Button2 = new JButton("按钮二");
//将按钮加入panel
add(Button1);
add(Button2);
//创建监听器
ButtonAction ButtonAction1 = new ButtonAction("按钮一");
ButtonAction ButtonAction2 = new ButtonAction("按钮二");
//注册监听器
Button1.addActionListener(ButtonAction1);
Button2.addActionListener(ButtonAction2);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); //显示用户的点击操作
g.drawString(" " + ButtonText, 90, 120);
System.out.println();
}
private class ButtonAction
implements ActionListener
{
public ButtonAction(String text)
{
clicktext =text;
}
public void actionPerformed(ActionEvent event)
{
ButtonText += ("您点击的是:"+clicktext );
repaint();
}
private String clicktext="";
} public String ButtonText = "";
}哪位兄弟能帮我改一下程序,让每次点一下结果会换行,而不是一直往后面添加
行不行? 我没试过 = =
http://club.pchome.net/topic_5_60_125240_1__.html
import java.awt.event.*;
import javax.swing.*;public class ButtonClick {
public static void main(String[] args) {
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class ButtonFrame extends JFrame {
public ButtonFrame() {
setTitle("ButtonClick");
setSize(WIDTH, HEIGHT); // 将panel加入到frame
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
} public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}class ButtonPanel extends JPanel {
JTextArea area=null;
public ButtonPanel() {
// 创建按钮
JButton Button1 = new JButton("按钮一");
JButton Button2 = new JButton("按钮二"); // 将按钮加入panel
add(Button1);
add(Button2); // 创建监听器
ButtonAction ButtonAction1 = new ButtonAction("按钮一");
ButtonAction ButtonAction2 = new ButtonAction("按钮二"); // 注册监听器
Button1.addActionListener(ButtonAction1);
Button2.addActionListener(ButtonAction2);
area=new JTextArea(20,40);
area.setEditable(false);
JScrollPane pane=new JScrollPane(area);
add(pane);
// area.setText("aaaaaaaaaaa\naaaaaaaaaaaa");
} public void paintComponent(Graphics g) {
super.paintComponent(g); // 显示用户的点击操作
// g.drawString(" " + ButtonText, 90, 120);
// g.drawChars(ButtonText.toCharArray(), 0, ButtonText.toCharArray().length, 90, 120);
// g.drawString("aaaaa\r\naaaaaaaa", 90, 122);
area.setText(ButtonText);
System.out.println();
} private class ButtonAction implements ActionListener {
public ButtonAction(String text) {
clicktext = text;
} public void actionPerformed(ActionEvent event) {
ButtonText += ("您点击的是:" + clicktext+"\n");
repaint();
} private String clicktext = ""; } public String ButtonText = "";}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//从Vector中取出所有字符串,分别drawstring,请自行根据字体大小算出每行字的height坐标,暂时用20代替
for (int i = 0; i < ButtonText.size(); i++) {
//显示用户的点击操作
g.drawString(" " + ButtonText.get(i), 90, 120 + 20*i);
}
System.out.println();
}//每点击一次,将String添加到Vector中去
public void actionPerformed(ActionEvent event)
{
ButtonText.add("您点击的是:"+clicktext);
repaint();
}
public Vector<String> ButtonText = new Vector<String>();