
正文
java中建立挡板代码 java中建立挡板代码是什么
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
求大神用Java编写出这个程序,要有具体代码,万分感激,定采纳
按照java中建立挡板代码你java中建立挡板代码的要求编写的JavaGUI程序如下:
import java.awt.GridLayout;
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.JTextField;
public class HH extends JFrame implements ActionListener{
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JTextField jtf=new JTextField(20);
JButton jb1=new JButton("显示");
JButton jb2=new JButton("清除");
HH(){
jb1.addActionListener(this);
jb2.addActionListener(this);
jp1.setLayout(new GridLayout(2,1));
jp3.add(jtf);
jp2.add(jb1);jp2.add(jb2);
jp1.add(jp3);jp1.add(jp2);
getContentPane().add(jp1);
setSize(300, 120);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1){
jtf.setText("java程序设计");
}
if(e.getSource()==jb2){
jtf.setText("");
}
}
public static void main(String[] args) {
new HH();
}
}
相关问答
Q1: java中做一个按钮,点击按钮后画一个矩形的代码怎么写?
兄弟帮你写了一个:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("画圆");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}
Q2: Java中怎么新建窗口?我是新手麻烦代码中主要语句解释一下
不知道是不是你说的窗口
使用javaswing JFrame设计窗口 + 布局就可实现,,如下例(添加了详细注释):
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class JFrameTest extends JFrame implements ActionListener {
private static final long serialVersionUID = -2829899643559384548L;
private JButton b1 = null;//按钮
private JTextArea jta = null;//文本
public JFrameTest() {
Container c = this.getContentPane();
c.setLayout(new BorderLayout());//设置布局方式,BorderLayout东西南北中布局
b1 = new JButton("点击");
b1.addActionListener(this);//为按钮添加监听
c.add(b1, BorderLayout.SOUTH);//添加按钮到c容器中,并分配在容器南(下)方
jta = new JTextArea();
c.add(jta, BorderLayout.CENTER);//添加文本区到c容器中,并分配在居中位置
this.setTitle("按钮事件");//设置窗口标题
this.setSize(300, 300);//设置窗体大小
this.setVisible(true);//窗体设置为显示
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体
//常用的一种关闭窗体的方法
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
//使用判断按钮名称的方法触发事件
if("点击".equals(e.getActionCommand())) {
jta.setText("按钮被点击了!");
}
//也可以获取对象名实现判断
// if(e.getSource() == b1) {
// jta.setText("按钮使用getSource方法被点击了!");
// }
}
public static void main(String[] args) {
new JFrameTest();
}
}
Q3: Android如何在java代码中设置margin?
1、比如imageViewjava中建立挡板代码,有一个getLayout方法,获得的layout在强转类型到LinearLayout或者其java中建立挡板代码他,然后再设定margin什么的。
2、我们平常可以直接在xml里设置margin,如java中建立挡板代码:
Xml代码 ImageView android:layout_margin="5dip" android:src="@drawable/image" /
但是有些情况下,需要在java代码里来写,可是View本身没有setMargin方法,怎么办呢?
通过查阅android api,我们发现android.view.ViewGroup.MarginLayoutParams有个方法
setMargins(left, top, right, bottom)。
其直接的子类有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams。
Q4: java 中挡板是什么?
在一些跨系统的性能测试项目中,往往由于客观因素的限制(测试硬件资源有限、多系统之间的协调等),我们无法搭建一个完整的测试环境来完成测试工作。此时,我们一般会搭建出被测系统,然后采用软件程序来模拟其他相关系统的功能。该软件程序一般被称为挡板。
关于java中建立挡板代码和java中建立挡板代码是什么的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







