Sunday, October 2, 2016

[Swing][Resolved] JMenuBar items disappearing behind panel


The problem is JMenuBar item hide behind a Label element,  and this is the code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class USD2EUR extends JFrame implements ActionListener{
    private JMenuBar  menuBar;
    private JMenu     mAction;
    private JMenuItem mConvert;
    private JMenuItem mExit;
    private Label eurResultLabel;
    private JTextField usdTextField;
    private double exchangeRate = 0.89;
   
    public USD2EUR(){
        JFrame frame = new JFrame("USD2EUR");
        frame.setLayout(new BorderLayout());
        setPreferredSize(new Dimension(200, 115));
       
        //MenuBar
        menuBar  = new JMenuBar();
        setJMenuBar(menuBar);
        mAction  = new JMenu("Action");
        menuBar.add(mAction);
       
        mConvert = new JMenuItem("Convert");
        mAction.add(mConvert);
        mConvert.addActionListener(this);
       
        mExit    = new JMenuItem("Exit");
        mAction.add(mExit);
        mExit.addActionListener(this);       
       
        //Content
        JPanel panel   = new JPanel();
        panel.setLayout(new GridLayout(2, 1));
        usdTextField   = new JTextField("USD Amount");
        panel.add(usdTextField);
        eurResultLabel = new Label("Label");
        panel.add(eurResultLabel);
        add(panel);       
       
        setVisible(true); //frame.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == mConvert){
            double usdAmount = Double.parseDouble(usdTextField.getText());
            double eurAmount = usdAmount * exchangeRate;
            eurResultLabel.setText(Double.toString(eurAmount));
        }else if(source == mExit){
            System.exit(0);
        }
    }   
   
    public static void main(String[] args) {         
        new USD2EUR();
    }
   
}
According Andrew Thompson in stackoverflow.com : "Don't mix Swing with any AWT components unless you have very good reason to do so.", so i change the Label (java.awt.Label) to a JLabel (javax.swing.JLabel).
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class USD2EUR extends JFrame implements ActionListener{
    private JMenuBar  menuBar;
    private JMenu     mAction;
    private JMenuItem mConvert;
    private JMenuItem mExit;
    private JLabel eurResultLabel;
    private JTextField usdTextField;
    private double exchangeRate = 0.89;
   
    public USD2EUR(){
        JFrame frame = new JFrame("USD2EUR");
        frame.setLayout(new BorderLayout());
        setPreferredSize(new Dimension(200, 115));
       
        //MenuBar
        menuBar  = new JMenuBar();
        setJMenuBar(menuBar);
        mAction  = new JMenu("Action");
        menuBar.add(mAction);
       
        mConvert = new JMenuItem("Convert");
        mAction.add(mConvert);
        mConvert.addActionListener(this);
       
        mExit    = new JMenuItem("Exit");
        mAction.add(mExit);
        mExit.addActionListener(this);       
       
        //Content
        JPanel panel   = new JPanel();
        panel.setLayout(new GridLayout(2, 1));
        usdTextField   = new JTextField("USD Amount");
        panel.add(usdTextField);
        eurResultLabel = new JLabel("Label");
        panel.add(eurResultLabel);
        add(panel);       
       
        setVisible(true); //frame.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == mConvert){
            double usdAmount = Double.parseDouble(usdTextField.getText());
            double eurAmount = usdAmount * exchangeRate;
            eurResultLabel.setText(Double.toString(eurAmount));
        }else if(source == mExit){
            System.exit(0);
        }
    }   
   
    public static void main(String[] args) {         
        new USD2EUR();
    }
   
}

Result: 

Reference

http://stackoverflow.com/questions/10107203/jmenubar-items-disappearing-behind-panel

2 comments :

  1. 你好。我想問下小生今年 23.如果想轉行做PROGAMMER
    有什麼要注意的地方? 因為我對CODE 係 基本上只是識少少HTML 同 CSS
    其他野完全5識。。如果用一兩年時間轉行,有無可能?

    ReplyDelete
    Replies
    1. 最好你有電腦的相關學歷, 因為很多programmer 的職位要求是有相關HD/ASSO/degree 畢業。23歲很年輕,轉行不會遲, 如果你可以用一至兩年時間讀到個相關的HD/ASSO, 一個top-up degree (快但要留意認受性) 又或是你已有其他類型的degree 再補個IT相關的master(我有同事咁做), 在你定立的時限內轉行不是沒可能,只要你有相關學歷就算零經驗也會有好多僱主肯俾機會。

      「因為我對CODE 係 基本上只是識少少HTML 同 CSS 其他野完全5識。」 <-- 建議你平平地報d 幾百至一千的短期課程 (例如Java course 學OOP concept)或去圖書館借一本program書認真學完一本,了解一下programming 為何物先(asp.net, php, javascript 什麼都可),因為有一d programmer 認為html, css太簡單冇咩logic 變化有限, 不當是program。最基本你要識if statement, for-loop, while-loop, function/method 等東西的用法。

      就我個人看法programmer 的入行門檻是「相關學歷」,未有肯定自己入不入行千萬不要花幾萬元去報讀長course, 因我在IVE時第一個學期就有不下3人頂不住退學了,建議你了解一下先, 及多問幾個人的意見比較一下。最後你要問自己入行是為興趣還是為糊口,如果對programming 沒興趣什至反感的話入行後會辛苦d 。

      Delete