给你一个 Core Java 2的例子 ImageViewer.java /** @version 1.21 2002-06-19 @author Cay Horstmann */import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*;/** A program for viewing images. */ public class ImageViewer { public static void main(String[] args) { JFrame frame = new ImageViewerFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } }/** A frame with a label to show an image. */ class ImageViewerFrame extends JFrame { public ImageViewerFrame() { setTitle("ImageViewer"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // use a label to display the images label = new JLabel(); Container contentPane = getContentPane(); contentPane.add(label); // set up the file chooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); // set up the menu bar JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem openItem = new JMenuItem("Open"); menu.add(openItem); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // show file chooser dialog int r = chooser.showOpenDialog(null); // if file selected, set it as icon of the label if(r == JFileChooser.APPROVE_OPTION) { String name = chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); } } }); JMenuItem exitItem = new JMenuItem("Exit"); menu.add(exitItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); } private JLabel label; private JFileChooser chooser; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 400; }
感觉挺简单的,我刚学java,如果你用过c++,就知道java创建界面有多简单了,呵呵
我也是初学,大家互相讨论学习!给你个简单的: import java.awt.*; import javax.swing.*; public class Trial { JTextField tf1,tf2; static JFrame f1; JPanel p1; public static void main(String args[]) { f1=new JFrame("Sample Frame"); Trial tobj=new Trial(); f1.setVisible(true); f1.setSize(300,300); } public Trial() { p1=new JPanel(); f1.getContentPane().add(p1); tf1=new JTextField(10); tf2=new JTextField(10); tf1.setText("Hello!World!"); Font myfont=new Font("Time New Roman", Font.BOLD,18); tf2.setFont(myfont); tf2.setText("Hello!World!"); p1.add(tf1); p1.add(tf2); } }
我这也有: import java.awt.*; import java.awt.event.*;public class Frame1 extends Frame { Frame1(String title) { super(title); setSize(350,350); Button button = new Button("你好"); add(button); setVisible(true); setLocation(300,200); } public static void main(String[] args) { new Frame1("我的第一个窗口!"); } } 首先提醒你,这个窗口关不了!(因为没添加事件)
ImageViewer.java
/**
@version 1.21 2002-06-19
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;/**
A program for viewing images.
*/
public class ImageViewer
{
public static void main(String[] args)
{
JFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A frame with a label to show an image.
*/
class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle("ImageViewer");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // use a label to display the images
label = new JLabel();
Container contentPane = getContentPane();
contentPane.add(label); // set up the file chooser
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(".")); // set up the menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar); JMenu menu = new JMenu("File");
menuBar.add(menu); JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
// show file chooser dialog
int r = chooser.showOpenDialog(null); // if file selected, set it as icon of the label
if(r == JFileChooser.APPROVE_OPTION)
{
String name
= chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
}); JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
} private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 400;
}
import java.awt.*;
import javax.swing.*;
public class Trial
{
JTextField tf1,tf2;
static JFrame f1;
JPanel p1;
public static void main(String args[])
{
f1=new JFrame("Sample Frame");
Trial tobj=new Trial();
f1.setVisible(true);
f1.setSize(300,300);
}
public Trial()
{
p1=new JPanel();
f1.getContentPane().add(p1);
tf1=new JTextField(10);
tf2=new JTextField(10);
tf1.setText("Hello!World!");
Font myfont=new Font("Time New Roman", Font.BOLD,18);
tf2.setFont(myfont);
tf2.setText("Hello!World!");
p1.add(tf1);
p1.add(tf2);
}
}
import java.awt.*;
import java.awt.event.*;public class Frame1 extends Frame
{
Frame1(String title)
{
super(title);
setSize(350,350);
Button button = new Button("你好");
add(button);
setVisible(true);
setLocation(300,200);
}
public static void main(String[] args)
{
new Frame1("我的第一个窗口!");
}
}
首先提醒你,这个窗口关不了!(因为没添加事件)