1.1) Write a main program that displays a singleframe with the title “My First Frame”. Set the size to 800 by 800. Make theframe visible.
1.2) Create a panel by using the JPanelconstructor and add it to the frame. Use Color.RED (a constant in the java.awtpackage) along with the setBackground() method inJPanel, to set the color of the panel. Add a JButton and a JLabel to the panelbefore adding the panel to the frame. Display the results.
1.3) The goal of this problem is to repackagemost of the previous code as a class that extends JFrame. Build a class called MyCustomFramethat extends JFrame. The class should contain instancevariables for the button and the label, as well as constants for the framewidth and height. The constructor should call a private helper method, createComponents(), that instantiates the button, the label, and the panel. The helpermethod should also add the button and the label to the panel, and add the panelto the frame. After creating the components, the constructor should set thesize of the frame.
Test your class with the viewer code below.
import javax.swing.JFrame;
public class MyCustomFrameViewer
{
public static void main(String[] args)
{
MyCustomFrame frame = new MyCustomFrame();
frame.setTitle("My first frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
1.4) Build a separate class called ClickListener that implements the ActionListenerinterface. Add an actionPerformed()method that prints the message “Button was clicked.” using System.out.println(). Create a ClickListener objectand register the object with the button by invoking addActionListener().
Test the program by clicking the button. Whenthe program is working correctly, convert the ClickListener class to an inner class in MyCustomFrame and test the program again.
1.5) Add a JTextField and a JTextArea to the frame. Initialize the field to an empty stringand the text area to “||”. Modify the program so that the contents of the fieldare appended to the area each time the user clicks the button. For example, ifthe user enters “xxx” in the field and clicks the button three times, the areacontains “||xxx||xxx||xxx||”.
Java图形用户界面
答案:2 悬赏:70 手机版
解决时间 2021-12-19 11:41
- 提问者网友:嘚啵嘚啵
- 2021-12-18 16:58
最佳答案
- 五星知识达人网友:从此江山别
- 2021-12-18 17:50
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyCustomFrameViewer {
public static void main(String[] args) {
MyCustomFrame frame = new MyCustomFrame();
frame.setTitle("My first frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyCustomFrame extends JFrame {
final public static int WIDTH = 800;
final public static int HEIGHT = 800;
private JButton b;
private JLabel l;
private JPanel p;
private JTextField tf;
private JTextArea ta;
public MyCustomFrame() {
createComponents();
setSize(WIDTH, HEIGHT);
}
private void createComponents() {
b = new JButton("添加");
l = new JLabel("这个不知道干啥");
tf = new JTextField(5);
p = new JPanel();
p.add(l);
p.add(tf);
p.add(b);
p.setBackground(Color.RED);
add(p, BorderLayout.NORTH);
ta = new JTextArea("||");
ta.setLineWrap(true);
add(ta);
b.addActionListener(new ClickListener());
}
class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
if (!"".equals(tf.getText())) {
ta.append(tf.getText());
ta.append("||");
}
}
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyCustomFrameViewer {
public static void main(String[] args) {
MyCustomFrame frame = new MyCustomFrame();
frame.setTitle("My first frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyCustomFrame extends JFrame {
final public static int WIDTH = 800;
final public static int HEIGHT = 800;
private JButton b;
private JLabel l;
private JPanel p;
private JTextField tf;
private JTextArea ta;
public MyCustomFrame() {
createComponents();
setSize(WIDTH, HEIGHT);
}
private void createComponents() {
b = new JButton("添加");
l = new JLabel("这个不知道干啥");
tf = new JTextField(5);
p = new JPanel();
p.add(l);
p.add(tf);
p.add(b);
p.setBackground(Color.RED);
add(p, BorderLayout.NORTH);
ta = new JTextArea("||");
ta.setLineWrap(true);
add(ta);
b.addActionListener(new ClickListener());
}
class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
if (!"".equals(tf.getText())) {
ta.append(tf.getText());
ta.append("||");
}
}
}
}
}
全部回答
- 1楼网友:零点过十分
- 2021-12-18 18:21
话说,;⑤添加两个标签、一个文本行和一个“ok”按钮;⑥事件响应:点击“ok”按钮,能实现把第一个文本行中的信息显示在第二个文本行。
你不觉得你这话冲突么。。
---------------------------
import java.awt.button;
import java.awt.flowlayout;
import java.awt.frame;
import java.awt.textfield;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import java.awt.event.windowadapter;
import java.awt.event.windowevent;
public class win {
frame win;
textfield t1;
textfield t2;
button btn;
public win() {
win = new frame();
win.settitle("我的java图形用户界面");
win.setbounds(200, 200, 240, 150);
win.setlayout(new flowlayout());
win.addwindowlistener(new windowadapter() {
@override
public void windowclosing(windowevent e) {
system.exit(0);
}
});
t1 = new textfield(24);
t2 = new textfield(24);
btn = new button("ok");
btn.addmouselistener(new mouseadapter() {
@override
public void mouseclicked(mouseevent e) {
t2.settext(t1.gettext());
}
});
win.add(t1);
win.add(t2);
win.add(btn);
win.setvisible(true);
win.validate();
}
public static void main(string[] args) {
new win();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯