永发信息网

Java图形用户界面

答案:2  悬赏:70  手机版
解决时间 2021-12-19 11:41
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||”.
最佳答案
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("||");
}
}
}
}
}
全部回答
话说,;⑤添加两个标签、一个文本行和一个“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(); } }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
为什么直流力矩电动机需要做成圆盘状
我为什么而活着阅读答案
凌晨三点十分时辰属什么
单选题________ournationalfootballteamlost
按键精灵手机助手怎么制作小精灵
We don’t know the meeting will be held s
多选题我国经济工作中,必须克服经济建设中盲
西安市周至县救助管理站地址在什么地方?想过
Ourteacherloves________andwelove_
西安市高陵县救助管理站地址有知道的么?有点
读图回答问题.(1)从图看我国气候的分布具
月幌见踪影,墨池闻暗香是什么意思
“如果你恨他,就把他放到‘人肉搜索引擎’上
沉迷女色啥意思?
从U盘拷贝的win7系统拷到新U盘 电脑识别不出
推荐资讯
中通423981076554单号到达保定中转,下一站是
老河口市襄樊新华书店(老河口市经济局电力行
人到一个没去过的地方感觉到这个地方自己似曾
海仓区安全生产监督管理局地址有知道的么?有
限制性股票和股票期权的区别是什么
华美超市的天堂牌雨伞的标价是56元,这“56元
我儿子在学校里被同学推了一把,摔在地上一颗
茂名市体育局在什么地方啊,我要处理点事
与馨忆谐音英文名?帮帮忙了……
我手机配的耳机怎么插上去左边的声音会变小?
单选题南美大陆上,同纬度的东部和西部气候差
有哪些关于电子商务运营的实用、精华书籍值得
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?