簡體   English   中英

鼠標懸停之前,JRadioButton不會出現

[英]JRadioButton Will not appear until Mouse over

import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class IndicatorWindow implements ItemListener {

    JRadioButton RMA, EMA, SMA, Williams, Stochastic;
    JPanel IndPan, RadioPanel, title;
    JLabel Lab;
    JButton OK;

    public JPanel createContentPane() {
        JPanel GUI = new JPanel();
        GUI.setLayout(null);

        title = new JPanel();
        title.setLayout(null);
        title.setLocation(0, 0);
        title.setSize(500, 145);
        GUI.add(title);

        Lab = new JLabel("Please Select Indicator Type");
        Lab.setLocation(5, 0);
        Lab.setSize(200, 30);

        title.add(Lab);

        ButtonGroup bg1 = new ButtonGroup();

        RadioPanel = new JPanel();
        RadioPanel.setLayout(null);
        RadioPanel.setLocation(10, 30);
        RadioPanel.setSize(190, 220);
        GUI.add(RadioPanel);

        RMA = new JRadioButton("RMA");
        RMA.setLocation(0, 0);
        RMA.addItemListener(this);
        RMA.setSize(110, 20);
        bg1.add(RMA);
        RadioPanel.add(RMA);

        EMA = new JRadioButton("EMA");
        EMA.setLocation(0, 30);
        EMA.addItemListener(this);
        EMA.setSize(110, 20);
        bg1.add(EMA);
        RadioPanel.add(EMA);

        SMA = new JRadioButton("SMA");
        SMA.setLocation(0, 60);
        SMA.addItemListener(this);
        SMA.setSize(110, 20);
        bg1.add(SMA);
        RadioPanel.add(SMA);

        Stochastic = new JRadioButton("Stochastic");
        Stochastic.setLocation(0, 90);
        Stochastic.addItemListener(this);
        Stochastic.setSize(110, 20);
        bg1.add(Stochastic);
        RadioPanel.add(Stochastic);

        Williams = new JRadioButton("Williams");
        Williams.setLocation(0, 120);
        Williams.addItemListener(this);
        Williams.setSize(110, 20);
        bg1.add(Williams);
        RadioPanel.add(Williams);

        OK = new JButton();
        OK.setText("Confirm");
        OK.setLocation(45, 150);
        OK.addItemListener(this);
        OK.setSize(90, 30);
        RadioPanel.add(OK);

        //GUI.setOpaque(true);
        return GUI;

    }

    public void itemStateChanged(ItemEvent e) {
        Object source = e.getItemSelectable();
        if (source == RMA) {
            System.out.print("Browse");
        } else if (source == EMA) {
            System.out.print("EMA");
        } else if (source == SMA) {
            System.out.print("SMA");
        } else if (source == Williams) {
            System.out.print("Williams");
        } else if (source == Stochastic) {
            System.out.print("Stochastic");
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Indicators");

        IndicatorWindow ind = new IndicatorWindow();
        frame.setContentPane(ind.createContentPane());

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(200, 250);
        frame.setLayout(null);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);

        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
        frame.setState(Frame.NORMAL);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }
}

我的問題是,當我編譯並運行此代碼時,jFrame出現了,但是只有一個問題,直到您將鼠標放在它們上面,才會出現3個JRadioButtons。 RMA和Williams單選按鈕出現,中間的3個沒有出現,為什么會這樣呢?

http://i.stack.imgur.com/gNnIb.jpg

您應該使用布局管理器。 人們認為使用“空布局”更容易,但實際上並非如此,並且您更容易出現代碼錯誤。 布局管理器將正確定位和調整組件的大小,以確保顯示所有組件。 有時,您甚至使用多個不同的布局管理器來獲得所需的布局。

在這種情況下,您的問題是您有兩個組件在容器中占據了相同的空間。 因此,一個組件會在另一個組件之上繪制。 將鼠標懸停在單選按鈕上之后,由於按鈕的翻轉效果,該按鈕將重新繪制。 但是,現在嘗試調整框架的大小,單選按鈕將消失,因為所有組件均已重新繪制,並且該組件再次繪制在按鈕上方。

下面的代碼行就是問題所在:

// title.setSize(500, 145);
title.setSize(500, 20);

但是真正的解決方案是重寫代碼並使用布局管理器。 在使用時,請使用正確的Java命名約定。 變量名不能以大寫字母開頭。 您正確設置了“標題”和“ bg1”。 因此,修復“ EMA”,“ RMA”等...

@camickr是正確的。 注意使用布局管理器(以及一些重構)如何實際上可以簡化您的代碼。 另外,相關教程建議使用操作偵聽器,而不是項目偵聽器。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5255337 */
public class IndicatorWindow implements ActionListener {

    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
    JRadioButton rma, ema, sma, stochastic, williams;
    ButtonGroup bg = new ButtonGroup();

    public JPanel createContentPane() {
        JPanel gui = new JPanel(new BorderLayout());

        JPanel title = new JPanel();
        JLabel lab = new JLabel("Please Select Indicator Type");
        title.add(lab);
        gui.add(title, BorderLayout.NORTH);

        createRadioButton(rma, "RMA");
        createRadioButton(ema, "EMA");
        createRadioButton(sma, "SMA");
        createRadioButton(stochastic, "Stochastic");
        createRadioButton(williams, "Williams");
        gui.add(radioPanel, BorderLayout.CENTER);

        JButton ok = new JButton();
        ok.setText("Confirm");
        ok.addActionListener(this);
        radioPanel.add(ok);

        return gui;
    }

    private void createRadioButton(JRadioButton jrb, String name) {
        jrb = new JRadioButton(name);
        bg.add(jrb);
        jrb.addActionListener(this);
        radioPanel.add(jrb);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Indicators");

        frame.add(new IndicatorWindow().createContentPane());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setAlwaysOnTop(true);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

您應該使用以下方法添加JRadioButtons:

private void bgAdd (String name, int y)
{
    JRadioButton rb = new JRadioButton (name);
    rb.setLocation (0, y);
    rb.addItemListener (this);
    rb.setSize (110, 19);
    bg1.add (rb);
    radioPanel.add (rb);    
}

調用代碼:

    bgAdd ("RMA", 0);
    bgAdd ("EMA", 30);
    bgAdd ("SMA", 60);
    bgAdd ("Stochastic", 90);
    bgAdd ("Williams",  120);

行動:

public void itemStateChanged (ItemEvent e) {
    Object button = e.getItemSelectable ();
    String source = ((JRadioButton) button).getText ();
    System.out.print (source + " ");
}

例如,然后將BoxLayout添加到頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM