求一道java用户界面设计题的代码

如题所述

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HelloFrame extends JFrame implements ActionListener{

public static void main(String[] args){

new HelloFrame();

}

private static final long serialVersionUID = 1L;

private int times;

protected JButton btnHello;

protected JButton btnClear;

protected JLabel lbCount;

protected JTextArea areaDisp;

public HelloFrame() {

super("Multilistener");
this.initFrame();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(320,200);
this.setVisible(true);
this.setResizable(false);
}

protected void initFrame() {

this.btnHello = new JButton("Hello!");
this.btnClear = new JButton("Clear");
this.lbCount = new JLabel("you have clicked:0 times",JLabel.CENTER);
this.areaDisp = new JTextArea();

JScrollPane pC = new JScrollPane(this.areaDisp);
JPanel pS = new JPanel();

pS.add(this.btnHello);
pS.add(this.btnClear);

this.add(this.lbCount,BorderLayout.NORTH);
this.add(pC,BorderLayout.CENTER);
this.add(pS,BorderLayout.SOUTH);

this.btnHello.addActionListener(this);
this.btnClear.addActionListener(this);


}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == this.btnHello){

this.areaDisp.append(this.times == 0 ? "Hello!" : "\r\nHello!");
this.lbCount.setText("you have clicked:"+(++this.times)+" times");
}

if(e.getSource() == this.btnClear){

this.times = 0;
this.areaDisp.setText("");
this.lbCount.setText("you have clicked:0 times");
}

}

}

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-06-01
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Demo
{
 private static JTextArea area;

 public static void main(String[] args)
 {
  JFrame jf=new JFrame();
  jf.setTitle("Multilistener");
  jf.setBounds(500, 200, 300, 300);
  JPanel con=new JPanel(null);
  
  area = new JTextArea();
  area.setLineWrap(true);
  JScrollPane jp=new JScrollPane(area);
  jp.setBounds(10, 10, 280, 200);
  con.add(jp);
  JButton helloButton=new JButton("Hello!");
  JButton clearButton=new JButton("clear");
  helloButton.addActionListener(new ActionListener()
  {
   
   @Override
   public void actionPerformed(ActionEvent e)
   {
    area.append("Hello!"+"\n");
   }
  });
  clearButton.addActionListener(new ActionListener()
  {
   
   @Override
   public void actionPerformed(ActionEvent e)
   {
    area.setText("");
   }
  });
  
  helloButton.setBounds(70, 220, 75, 30);
  clearButton.setBounds(150, 220, 75, 30);
  con.add(helloButton);
  con.add(clearButton);
  
  
  jf.add(con);
  jf.setResizable(false);
  jf.setVisible(true);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

第2个回答  2015-06-01
竟然还是英文的,稍等。追问

很简单 就是一个界面 点击按钮显示hello

追答

package say; import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.*; import javax.swing.*; public class SayHello extends JFrame { public static void main(String[] args) { new SayHello("SayHello").display();; } public void display(){ setVisible(true); } private int times; private JLabel label; private JTextArea textArea; private JButton hello; private JButton clear; private JScrollPane scrollPane ; public SayHello(String title){ super(title); init(); initLabel(); initTextArea(); initButton(); initListener(); } private void init(){ setResizable(false); setSize(400, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void initLabel(){ label = new JLabel("you have clicked " + times +" times",JLabel.CENTER); add(label, BorderLayout.NORTH); } private void initTextArea(){ textArea = new JTextArea(); textArea.setFont(new Font(Font.SERIF, Font.PLAIN, 18)); scrollPane = new JScrollPane(textArea); scrollPane.setAutoscrolls(true); add(scrollPane, BorderLayout.CENTER); } private void initButton(){ hello = new JButton("Hello!"); clear = new JButton("Clear"); JPanel pan = new JPanel(); pan.add(hello); pan.add(clear); add(pan, BorderLayout.SOUTH); } private void initListener(){ hello.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(times > 0) textArea.append("\n"); textArea.append("Hello!"); times++; updateTimes(times); } }); clear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { textArea.setText(""); times = 0; updateTimes(times); } }); } private void updateTimes(int times){ label.setText("you have clicked " + times +" times"); } }

相似回答