使用JTree 給你個例子import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class TreeDemo1{
public TreeDemo1(){
JFrame f=new JFrame("TreeDemo1");
Container contentPane=f.getContentPane();
String[] s1={"公司文件","個人信件","私人文件"};
String[] s2={"本機磁碟(C:)","本機磁碟(D:)","本機磁碟(E:)"};
String[] s3={"奇摩站","職棒消息","網路書店"};
Hashtable hashtable1=new Hashtable();
Hashtable hashtable2=new Hashtable();
hashtable1.put("我的公文包",s1);
hashtable1.put("我的電腦",s2);
hashtable1.put("收藏夾",hashtable2);
hashtable2.put("網站列表",s3);
Font font = new Font("Dialog", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
/**定義widnows界面**/
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception el){
System.exit(0);
}
/**定義widnows界面**/
JTree tree=new JTree(hashtable1);
JScrollPane scrollPane=new JScrollPane();
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args){
new TreeDemo1();
}
}
B. java 可視化界面編程
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class awttest
{
TextField id,pw;
TextField printid,printpw;
public awttest(){
Frame f=new Frame("用戶登錄");
f.setLayout(new GridLayout(4,2));
id=new TextField("輸入用戶名",10);
pw=new TextField(10);
pw.setEchoChar('*');
f.add(new Label("用戶名:",Label.CENTER));
f.add(id);
f.add(new Label("密碼:",Label.CENTER));
f.add(pw);
Button b1=new Button("登陸");
Button b2=new Button("取消");
f.add(b1);
f.add(b2);
String str1=id.getText();
String str2=pw.getText();
printid=new TextField(str1,10);
printpw=new TextField(str2,10);
f.add(printid);
f.add(printpw);
f.pack();
printid.setBackground(new Color(220,0,0));
printpw.setBackground(new Color(220,0,0));
f.setSize(250,120);
f.setVisible(true);
b1.addActionListener(new ActionListener() { // 點擊「顯示窗口」菜單後將窗口顯示出來
public void actionPerformed(ActionEvent e) {
printid.setText(id.getText());
printpw.setText(pw.getText());
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent args)
{
System.exit(0);
}
}
);
}
public static void main(String[] args){
new awttest();
}
}
OK!!可以運行
C. 求java窗口程序編程
package window;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Window extends JFrame{
public static int num = 0;
public static void main(String[] args) {
new Window();
}
public Window(){
//設置x y坐標
this.setLocation(400, 500);
//設置窗口大小
this.setSize(512, 512);
//設置窗口可見
this.setVisible(true);
//設置窗口關閉後程序停止
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//設置窗口不可移動
this.setResizable(false);
JButton but1 = new JButton("按鈕");
final JTextField text1 = new JTextField(10);
D. java編寫一個圖形界面程序
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjavax.swing.*;
importjavax.swing.border.Border;
classMainFrameextendsJFrame{
=1L;
privateMap<String,Integer>sizes=newHashMap<String,Integer>();
privateMap<String,Integer>styles=newHashMap<String,Integer>();
privateMap<String,Integer>toppings=newHashMap<String,Integer>();
publicMainFrame(){
sizes.put("ExtraLarge",10);
sizes.put("Large",8);
sizes.put("Medium",5);
sizes.put("Small",3);
styles.put("DeepDish",20);
styles.put("Regular",10);
styles.put("ThinCrust",5);
styles.put("Chicago",3);
toppings.put("Cheese",8);
toppings.put("Tomato",7);
toppings.put("Peppers",6);
toppings.put("Peperoni",5);
this.setTitle("布局及事件處理");
this.setSize(450,350);
this.setLayout(newBorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabellblTitle=newJLabel();
lblTitle.setText("PizzeriaJuno");
lblTitle.setFont(newFont("宋體",Font.BOLD,36));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
this.add("North",lblTitle);
JPanelbodyPanel=newJPanel();
bodyPanel.setLayout(newGridLayout(2,1));
this.add("Center",bodyPanel);
JPanellistPanel=newJPanel();
listPanel.setLayout(newGridLayout(1,3));
listPanel.setSize(200,200);
bodyPanel.add(listPanel);
BorderlineBorder=BorderFactory.createLineBorder(Color.BLACK);
JPanelsizePanel=newJPanel();
sizePanel.setLayout(newBorderLayout());
listPanel.add(sizePanel);
JLabelsizeTitle=newJLabel();
sizeTitle.setText("Sizes");
sizePanel.add("North",sizeTitle);
JListsizeList=newJList(sizes.keySet().toArray());
sizeList.setSize(100,100);
sizeList.setBorder(lineBorder);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sizePanel.add(sizeList);
JPanelstylePanel=newJPanel();
stylePanel.setLayout(newBorderLayout());
listPanel.add(stylePanel);
JLabelstyleTitle=newJLabel();
styleTitle.setText("Styles");
stylePanel.add("North",styleTitle);
JListstyleList=newJList(styles.keySet().toArray());
styleList.setSize(100,100);
styleList.setBorder(lineBorder);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
stylePanel.add(styleList);
JPaneltoppingPanel=newJPanel();
toppingPanel.setLayout(newBorderLayout());
listPanel.add(toppingPanel);
JLabeltoppingTitle=newJLabel();
toppingTitle.setText("Toppings");
toppingPanel.add("North",toppingTitle);
JListtoppingList=newJList(toppings.keySet().toArray());
toppingList.setSize(100,100);
toppingList.setBorder(lineBorder);
toppingPanel.add(toppingList);
JTextAreatxtResult=newJTextArea();
txtResult.setEditable(false);
bodyPanel.add(txtResult);
JPanelbottomPanel=newJPanel();
bottomPanel.setLayout(newGridLayout(1,3));
this.add("South",bottomPanel);
JLabellabel1=newJLabel("Clicktocompleteorder");
bottomPanel.add(label1);
JButtonbtnRingUp=newJButton("Ringup");
btnRingUp.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
if(sizeList.getSelectedValue()==null){
JOptionPane.showMessageDialog(MainFrame.this,"Pleaseselectsize.");
return;
}
if(styleList.getSelectedValue()==null){
JOptionPane.showMessageDialog(MainFrame.this,"Pleaseselectstyle.");
return;
}
if(toppingList.getSelectedValue()==null){
JOptionPane.showMessageDialog(MainFrame.this,"Pleaseselecttopping.");
return;
}
floattotal=0;
Stringsize=sizeList.getSelectedValue().toString();
total+=sizes.get(size);
Stringstyle=styleList.getSelectedValue().toString();
total+=styles.get(style);
Stringresult=size+"Pizza,"+style+"Style";
Object[]toppings=toppingList.getSelectedValues();
for(Objecttopping:toppings){
result+="
+"+topping.toString();
total+=MainFrame.this.toppings.get(topping.toString());
}
result+="
Total:"+total;
txtResult.setText(result);
}
});
bottomPanel.add(btnRingUp);
JButtonbtnQuit=newJButton("Quit");
btnQuit.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
MainFrame.this.dispose();
}
});
bottomPanel.add(btnQuit);
}
}
publicclassApp{
publicstaticvoidmain(String[]args){
MainFramemainFrame=newMainFrame();
mainFrame.setVisible(true);
}
}
E. Java圖形用戶界面編程
我給你寫,已經測試,你看合不合你的意思
package dongzi.pattern;
import java.awt.FlowLayout;
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 SumTest extends JPanel{
JTextField mem[] = new JTextField[10] ;
JButton sumButton = new JButton("SUM") ;
JTextField sumText = new JTextField(4) ;
public SumTest() {
setLayout(new FlowLayout()) ;
for (int i = 0; i < 10; i++) {
mem[i] = new JTextField(4) ;
add(mem[i]) ;
}
add(sumButton) ;
add(sumText) ;
sumButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int sum = 0 ;
for (int i = 0 ;i < 10; i++) {
sum += Integer.parseInt(mem[i].getText().trim()) ;
}
sumText.setText(String.valueOf(sum)) ;
}
}) ;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame() ;
SumTest test = new SumTest() ;
frame.add(test) ;
frame.setSize(800, 100) ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
frame.setVisible(true) ;
}
}
F. 如何用JAVA編程編寫一個界面程序(急求!!!)
用swing組件來做,你可以用jbulid或則jcreate做為開發環境
G. java圖形界面編程
執行java Applt小程序不是這樣運行的。你必須建一個html文件,然後在輸入
<applet code="ButtonDemo.class"
width=320 height=180>
</applet>
其中code是你編譯ButtonDemo類是生成的class文件。然後在cmd中找到html文件所在的路徑下輸入appletviewer xxx.html
就可以運行了
H. java編程實現圖形界面
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
public class mi
{
private static String username;
private static String password ;
private static JTextField []t={
new JTextField("賬號:",8),new JTextField(10),
new JTextField("密碼:",8),new JPasswordField(10)};
public static void main (String args[]){
JFrame app=new JFrame("賬號密碼演示程序");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(280,120);
Container c=app.getContentPane();
c.setLayout(new FlowLayout());
t[0].setEditable(false);
t[2].setEditable(false);
for(int i=0;i<4;i++)
c.add(t[i]);t[1].setText("");
JButton[]b={new JButton("確定"),new JButton("重置")};
c.add(b[0]);c.add(b[1]);
app.setLocationRelativeTo(null);
app.setVisible(true);
b[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t[1].setText("");
t[3].setText("");
}
});
// 登錄按鈕加事件監聽器
b[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username = t[1].getText();
password = t[3].getText();
//判斷用戶名密碼是否正確
if (username.equals("數字") && password.equals("123")) {
JOptionPane.showMessageDialog(null, "登陸成功!", "消息",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!", "錯誤",
JOptionPane.ERROR_MESSAGE);
}
}
}); }
}
I. java 界面編程
public class test extends JFrame implements ActionListener{
private JTextField jtf;
private JButton jbPlain;
private JButton jbBold;
private JButton jbItaly;
public test(){
super("test application");
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void init(){
jtf = new JTextField("ceshi");
jbPlain = new JButton("putong");
jbBold = new JButton("cuti");
jbItaly = new JButton("xieti");
Container c = this.getContentPane();
c.setLayout(new GridLayout(2,3));
JPanel jp1 = new JPanel();
jp1.add(jtf);
JPanel jp2 = new JPanel();
jp2.add(jbPlain);
jp2.add(jbBold);
jp2.add(jbItaly);
c.add(jp1);
c.add(jp2);
jbPlain.addActionListener(this);
jbBold.addActionListener(this);
jbItaly.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof JButton){
if(e.getSource().equals(jbPlain)){
jtf.setFont(new Font("wo",Font.PLAIN, 12));
}
if(e.getSource().equals(jbBold)){
jtf.setFont(new Font("wo",Font.BOLD, 12));
}
if(e.getSource().equals(jbItaly)){
jtf.setFont(new Font("wo", Font.ITALIC, 12));
}
}
}
public static void main(String []args){
new test();
}
}
J. java編程用什麼做界面設計
Java的界面設計很大一部分都是利用編程工具(有NetBeans,MyEclipse等等),裡面有界面編程類,新建後可以直接拖拽組件(按鈕,文本框等),可自動生成代碼,極大的減少了程序員的編寫代碼量
所以我還是建議你下載一個編程工具,還體驗一下,很好懂得,建議NetBeans