frank 發問時間: 電腦與網際網路程式設計 · 1 0 年前

java 為什麼第一次按項目時,按鈕顯示不出來,第二次就可以

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class menu_1 extends JFrame

{

private JButton button1,button2; // manages color menu items

private JPanel panel1,panel2;

private JFrame frame1,frame2;

// no-argument constructor set up GUI

public menu_1()

{

super( "Using JMenus" );

setLayout(new FlowLayout());

button1 = new JButton("test1");

JMenu buttonMenu = new JMenu( "顯示按鈕");

buttonMenu.setMnemonic( 'B' );

JMenuItem b1 = new JMenuItem( "顯示按鈕1" );

b1.setMnemonic( '1' );

buttonMenu.add( b1 );

b1.addActionListener(

new ActionListener() // anonymous inner class

{

// display message dialog when user selects About...

public void actionPerformed( ActionEvent event )

{

add( button1);

button1.setVisible(true);

button2.setVisible(false);

getContentPane().setBackground( Color.YELLOW );

} // end method actionPerformed

} // end anonymous inner class

); // end call to addActionListener

button2 = new JButton("test2");

JMenuItem b2 = new JMenuItem( "顯示按鈕2" );

b2.setMnemonic( '2' );

buttonMenu.add(b2 );

b2.addActionListener(

new ActionListener() // anonymous inner class

{

// terminate application when user clicks exitItem

public void actionPerformed( ActionEvent event )

{

add( button2);

button2.setVisible(true);

button1.setVisible(false);

getContentPane().setBackground( Color.CYAN );

} // end method actionPerformed

} // end anonymous inner class

); // end call to addActionListener

JMenuBar bar = new JMenuBar(); // create menu bar

setJMenuBar( bar ); // add menu bar to application

bar.add( buttonMenu ); // add file menu to menu bar

} // end Menu_1 constructor

public static void main( String args[] )

{

menu_1 menuFrame = new menu_1(); // create MenuFrame

menuFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

menuFrame.setSize( 500, 200 ); // set frame size

menuFrame.setVisible( true ); // display frame

} // end main

} // end class Menu_1

2 個解答

評分
  • 1 0 年前
    最佳解答

    You need to UpdateUI for button1 and button2. in ActionListener method

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    public class Menu_1 extends JFrame

    {

    private JButton button1,button2;

    private JPanel panel1,panel2;

    private JFrame frame1,frame2;

    // no-argument constructor set up GUI

    public Menu_1()

    {

    super( "Using JMenus" );

    setLayout(new FlowLayout());

    button1 = new JButton("test1");

    JMenu buttonMenu = new JMenu( "Taiwan顯示按鈕");

    buttonMenu.setMnemonic( 'B' );

    JMenuItem b1 = new JMenuItem( "Japan顯示按鈕1" );

    b1.setMnemonic( '1' );

    buttonMenu.add( b1 );

    b1.addActionListener(

    new ActionListener() // anonymous inner class

    {

    // display message dialog when user selects About...

    public void actionPerformed( ActionEvent event )

    {

    add( button1);

    getContentPane().setBackground( Color.YELLOW );

    button1.setVisible(true);

    button2.setVisible(false);

    button1.updateUI(); //added by DYU

    } // end method actionPerformed

    } // end anonymous inner class

    ); // end call to addActionListener

    button2 = new JButton("test2");

    JMenuItem b2 = new JMenuItem( "Korea顯示按鈕2" );

    b2.setMnemonic( '2' );

    buttonMenu.add(b2 );

    b2.addActionListener(

    new ActionListener() // anonymous inner class

    {

    // terminate application when user clicks exitItem

    public void actionPerformed( ActionEvent event )

    {

    add( button2);

    button2.setVisible(true);

    button1.setVisible(false);

    getContentPane().setBackground( Color.CYAN );

    button2.updateUI(); //Added by DYU

    }

    }

    ); // end call to addActionListener

    JMenuBar bar = new JMenuBar(); // create menu bar

    setJMenuBar( bar ); // add menu bar to application

    bar.add( buttonMenu ); // add file menu to menu bar

    } // end Menu_1 constructor

    public static void main( String args[] )

    {

    Menu_1 menuFrame = new Menu_1(); // create MenuFrame

    menuFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    menuFrame.setSize( 1000, 400 ); // set frame size

    menuFrame.setVisible( true ); // display frame

    }

    }

    • Commenter avatar登入以對解答發表意見
  • 7 年前

    如果你很確定自己很想結婚

    婚友社的確是一個有效率的管道

    年紀越大壓力越大

    以前我去婚友社時

    年輕的女生機會總是比較多

    時間總是不站在女生這邊的

    所以如果你想婚的念頭已經確定

    就去找一家評價好一點的婚友社!

    行動吧!

    這是我之前參加的婚友社我覺得不錯

    或搜尋"紅娘李姐"

    不過還是建議你自己去諮詢過看是否適合你!

    • Commenter avatar登入以對解答發表意見
還有問題?馬上發問,尋求解答。