小簡
Lv 6
小簡 asked in 電腦與網際網路程式設計 · 1 decade ago

java程式求助

最近有一個程式

是要寫

圖片參考:http://imgcld.yimg.com/8/n/AE03676736/o/1010050603...

從此圖可知上半部是Border Layout下半部6格是Grid Layout

那麼問題來了要怎麼把這2個不同的效果的程式容合為一個

裡面bottom編號可以順序不一樣

import java.awt.*;

import java.awt.event.*;

public class BorderLayoutDemo extends java.awt.Frame {

public static void main(String args[]){

new BorderLayoutDemo();

}

public BorderLayoutDemo() {

super("Border Layout Demo");

Button button1 = new Button("EAST");

Button button2 = new Button("SOUTH");

Button button3 = new Button("WEST");

Button button4 = new Button("NORTH");

Button button5 = new Button("CENTER");

add(button1, BorderLayout.EAST);

add(button2, BorderLayout.SOUTH);

add(button3, BorderLayout.WEST);

add(button4, BorderLayout.NORTH);

add(button5, BorderLayout.CENTER);

}

}

public class GridLayoutDemo extends java.awt.Frame {

public static void main(String args[]){

new GridLayoutDemo();

}

public GridLayoutDemo() {

super("Grid Layout Demo");

setLayout(new GridLayout(3, 3));

for (int i=0; i<row; i++) {

for (int j=0; j<column; j++) {

add(new java.awt.Button("(" + i + ", " + j + ")"));

}

}

this.setSize(200, 200);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = this.getSize();

if (frameSize.height > screenSize.height)

frameSize.height = screenSize.height;

if (frameSize.width > screenSize.width)

frameSize.width = screenSize.width;

this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

this.setVisible(true);

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

}

我是弄成這樣,但還不完全的程式,還請各位大大高手門

幫我看看該怎麼精簡一下,還有那邊部分有錯,還有怎麼融合?

謝謝

Update:

原來是用panel

Update 2:

檔名要叫什麼?

另外Panel 還要在宣告這東西嗎?

Update 3:

set path=;"C:\Program Files\Java\jdk1.6.0_17\bin";

set classpath=.;"C:\Program Files\Java\jdk1.6.0_17\jre\bin\rt.jar"

Update 4:

javac MyFrame.java

java MyFrame

javac BorderLayoutDemo.java

java BorderLayoutDemo

javac GridLayoutDemo.java

java GridLayoutDemo

pause

Update 5:

上面這個是我的批次檔,編繹寫這樣,還缺什麼?還是這樣寫是不對的

Update 6:

我按批次檔執行後還是跑不出我前面打的那張圖

Update 7:

int row=3;

int column=3;

setLayout(new GridLayout(row, column));

這段好像可以精簡成 setLayout(new GridLayout(3 3));

Update 8:

所以批次檔的檔的名是MyFrame.bat

set path="C:\Program Files\Java\jdk1.6.0_17\bin";

set classpath=.;"C:\Program Files\Java\jdk1.6.0_17\jre\bin\rt.jar"

javac MyFrame.java

java BorderLayoutDemo

java GridLayoutDemo

java MyFrame

pause

Update 9:

程式的部分檔名是MyFrame.java 嚕?

有點不懂的是

這些你打的有加<>這個符號是?註解嗎?還是有功用的?

Update 10:

BorderLayoutDemo.java

下面為什麼重覆「<」BorderLayoutDemo.java「>」

是打錯還是要打成「<」GridLayoutDemo.java「>」

抱歉我得打「」<---這個符號加進去,這個知識家發言會變成空白

Update 11:

那如果照你這樣說

三個程式是分開寫?還是集中在一個程式之中

我要的是最終跑出結果是,那個我 上面畫的圖

應該是這3個程式,寫在同一個程式之中吧

3 Answers

Rating
  • Cola
    Lv 5
    1 decade ago
    Favorite Answer

    改為繼承Panel彈性比較大

    建構方法多兩個參數,可以設定大小

    <MyFrame.java>

    import java.awt.*;

    import java.awt.event.*;

    public class MyFrame{

    public static void main(String args[]){

    Frame f = new Frame();

    f.add(new BorderLayoutDemo(500,400),BorderLayout.NORTH);

    f.add(new GridLayoutDemo(500,200),BorderLayout.CENTER);

    f.pack();

    Dimension frameSize = f.getSize();

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    f.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

    f.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

    System.exit(0);

    }

    });

    f.setVisible(true);

    }

    }

    <BorderLayoutDemo.java>

    import java.awt.*;

    import java.awt.event.*;

    public class BorderLayoutDemo extends Panel {

    public static void main(String args[]){

    Frame f = new Frame();

    f.add(new BorderLayoutDemo(500,500));

    f.pack();

    f.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

    System.exit(0);

    }

    });

    f.setVisible(true);

    }

    public BorderLayoutDemo(int w,int h) {

    setLayout(new BorderLayout());

    Button button1 = new Button("EAST");

    ....................

    ....................

    add(button5, BorderLayout.CENTER);

    setPreferredSize(new Dimension(w, h));

    }

    }

    <BorderLayoutDemo.java>

    import java.awt.*;

    import java.awt.event.*;

    public class GridLayoutDemo extends Panel {

    public static void main(String args[]){

    Frame f = new Frame();

    f.add(new GridLayoutDemo(200,200));

    f.pack();

    f.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

    System.exit(0);

    }

    });

    f.setVisible(true);

    }

    public GridLayoutDemo(int w,int h) {

    int row=3;

    int column=3;

    setLayout(new GridLayout(row, column));

    for (int i=0; i<row; i++) {

    for (int j=0; j<column; j++) {

    add(new Button("(" + i + ", " + j + ")"));

    }

    }

    setPreferredSize(new Dimension(w, h));

    }

    }

    2010-05-07 15:52:23 補充:

    補充內容po不上去,改po這裡

    檔名就跟public class 定義的一樣

    若要宣告就是

    BorderLayoutDemo b;

    GridLayoutDemo g;

    2010-05-07 15:53:40 補充:

    版大若要用批次檔就是

    @ECHO OFF

    set path=C:\\Program Files\\Java\\jdk1.6.0_17\\bin

    set classpath=.;C:\\Program Files\\Java\\jdk1.6.0_17\\jre\\bin\\rt.jar

    @ECHO Compiling ...

    javac MyFrame.java

    @ECHO Run ...

    java BorderLayoutDemo

    java GridLayoutDemo

    java MyFrame

    @ECHO ON

    2010-05-07 15:54:54 補充:

    編譯MyFrame.java就可以了

    編譯MyFrame.java的時候就會產生BorderLayoutDemo跟GridLayoutDemo

    2010-05-07 16:05:44 補充:

    setLayout(new GridLayout(row, column));

    setLayout(new GridLayout(3, 3));

    兩個其實都可以

    只不過當你要修改的時候,只需修改row和column兩個變數

    不需再修改迴圈的地方

    2010-05-07 21:56:13 補充:

    上面只是標明檔名,非程式碼的一部份,

    是應該給它//開頭,sorry造成版大誤解

    不過第三個檔名po錯,應為GridLayoutDemo.java

    2010-05-07 23:54:29 補充:

    //寫在同一個檔案

    public class MyFrame{

    public static void main(String args[]){

    new MyFrame();

    }

    public MyFrame(){

    //原本main裡的內容移到這裡

    }

    //下面兩個類別都刪掉原本的main方法,public去掉

    class BorderLayoutDemo extends Panel {

    .........

    }

    class GridLayoutDemo extends Panel {

    ........

    }

    }

    //上面兩個類別也可以放到這裡

    2010-05-10 12:11:15 補充:

    MyFrame只是一個自定義類別

    JAVA吉他手大大指的是Swing

    JAVA有兩種視窗套件

    Awt元件(heavyweight compoent)

    舊的視窗套件,不能跨平台,耗費系統資源,元件少

    Swing元件(lightweight compoent)

    新的視窗套件,純java code,跨平台,少耗系統資源,可用資源多

    我也覺得改Swing比較好,若要變更為Swing,只要把Awt元件加上J開頭

    Frame改JFrame

    Panel改JPanel

    Button改JButton

    上頭再加上import javax.swing.*;

  • 小簡
    Lv 6
    1 decade ago

    為何改成JFrame比較好?

    cola大大改成的是MyFrame

    2010-05-10 13:48:51 補充:

    原來如此

    了解

    我已經能跑出來了

  • 吉他
    Lv 5
    1 decade ago

    建議把Frame改成JFrame比較好

Still have questions? Get your answers by asking now.