
JAVA GUI 程序
编写一个GUI程序,界面中有2个按钮和一个文本框。按钮文本分别为“YES”和“NO”;点击第一个按钮,在文本框中显示”youpressedYes”,点击第二个按钮,在文本...
编写一个GUI程序,界面中有2个按钮和一个文本框。按钮文本分别为“YES”和“NO”;点击第一个按钮,在文本框中显示”you pressed Yes” ,点击第二个按钮,在文本框中显示”you pressed No”
今晚要考试 还有我根本没有学啊....... 展开
今晚要考试 还有我根本没有学啊....... 展开
展开全部
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
/**
* @author Hardneedl
*/
final class ButtonDemo extends JFrame {
public String getTitle() {return "ButtonDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
private class ButtonAction extends AbstractAction{
private JTextComponent t;
private String n;
private ButtonAction(String name,JTextComponent t) {
super(name);
n=name;
this.t=t;
}
public void actionPerformed(ActionEvent e) {
t.setText(n);
}
}
private JButton b0,b1;
private JTextField t;
ButtonDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private void init(){
t=new JTextField(20);
b0=new JButton(new ButtonAction("YES",t));
b1=new JButton(new ButtonAction("NO",t));
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
JPanel p = new JPanel();
p.add(b0);
p.add(b1);
container.add(p,BorderLayout.NORTH);
container.add(t,BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new ButtonDemo();
}
}
);
}
}
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
/**
* @author Hardneedl
*/
final class ButtonDemo extends JFrame {
public String getTitle() {return "ButtonDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
private class ButtonAction extends AbstractAction{
private JTextComponent t;
private String n;
private ButtonAction(String name,JTextComponent t) {
super(name);
n=name;
this.t=t;
}
public void actionPerformed(ActionEvent e) {
t.setText(n);
}
}
private JButton b0,b1;
private JTextField t;
ButtonDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private void init(){
t=new JTextField(20);
b0=new JButton(new ButtonAction("YES",t));
b1=new JButton(new ButtonAction("NO",t));
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
JPanel p = new JPanel();
p.add(b0);
p.add(b1);
container.add(p,BorderLayout.NORTH);
container.add(t,BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new ButtonDemo();
}
}
);
}
}
2013-06-14
展开全部
package test;
import org.eclipse.swt.SWT;
public class aaa extends Shell {
private Text text;
/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
aaa shell = new aaa(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
* @param display
*/
public aaa(Display display) {
super(display, SWT.SHELL_TRIM);
Button btnYes = new Button(this, SWT.NONE);
btnYes.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText("");
text.setText("you pressed Yes");
}
});
btnYes.setBounds(10, 62, 72, 22);
btnYes.setText("Yes");
Button btnNo = new Button(this, SWT.NONE);
btnNo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText("");
text.setText("you pressed No");
}
});
btnNo.setBounds(131, 62, 72, 22);
btnNo.setText("No");
text = new Text(this, SWT.BORDER);
text.setBounds(10, 118, 232, 92);
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
import org.eclipse.swt.SWT;
public class aaa extends Shell {
private Text text;
/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
aaa shell = new aaa(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
* @param display
*/
public aaa(Display display) {
super(display, SWT.SHELL_TRIM);
Button btnYes = new Button(this, SWT.NONE);
btnYes.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText("");
text.setText("you pressed Yes");
}
});
btnYes.setBounds(10, 62, 72, 22);
btnYes.setText("Yes");
Button btnNo = new Button(this, SWT.NONE);
btnNo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText("");
text.setText("you pressed No");
}
});
btnNo.setBounds(131, 62, 72, 22);
btnNo.setText("No");
text = new Text(this, SWT.BORDER);
text.setBounds(10, 118, 232, 92);
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
太简单了吧?稍微学习哈就写出来了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这么简单自己写一个呗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询