菜单栏未出现在Java中

迈克尔·达伊尔

我用所有的变量制作并定义了菜单栏,而普通菜单不会显示。它可以很好地编译并轻松运行,但是菜单栏不存在...我将其放置在错误的位置了吗?Fyi:菜单栏位于代码的顶部附近,底部附近有一些复选框已添加到菜单栏。

GUI

        import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Locale;

import json.JsonObject;
public class dicebot extends JFrame implements ActionListener {
    static final long serialVersionUID = 1L;
    private JPanel contentPane;
    public static JCheckBox RollAmntCheck;
    public static JTextField txtRollAmnt;
    public static String APIKey = null;
    public static JComboBox<String>cmbCurrency;
    public static JMenuBar menuBar;
    public static JMenu menu;
    public static JMenuItem menuItem;
    public static JButton btnLow;
    public static JButton btnFloat;
    public static JButton btnHigh;
    public static JButton btnClearLog;
    public static JButton btnDonate;
    public static JButton btnCalc;
    public static JCheckBox AlwaysOnTopCheck;
    public static JCheckBox scrollCheck;
    public static JCheckBox scrollDisable;
    public static JTextField txtUserName;
    public static JTextField txtStartBid;
    public static JTextField txtMultiplier;
    public static JTextField txtMinRemaining;
    public static JPasswordField txtPassword;
    public static JTextField txtOdds;
    public static JTextField txtMaxBet;
    public static JTextArea txtInfo;
    public static JLabel lblBalTag;
    public static JLabel userTag;
    public static JLabel passTag;
    public static dicebot frame = null;
    public static void main(String[] args) {
        Locale.setDefault(Locale.US);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new dicebot();
                    frame.setVisible(true);
                    frame.setJMenuBar(menuBar);
                    Dicebotcode d = new Dicebotcode();
                    d.LoadSettings();
                    //d = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public dicebot() {
        setTitle("Dice Bot");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.WEST);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0};
        gbl_panel.rowHeights = new int[]{0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0};
        gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        menuBar = new JMenuBar();
        contentPane.add(menuBar);

        menu = new JMenu("Options");
        menu.getAccessibleContext().setAccessibleDescription("Menu Bar");
        menuBar.add(menu);

        menuItem = new JMenuItem("Menu");
        menuItem.getAccessibleContext().setAccessibleDescription("Menu");
        menu.add(menuItem);



        //Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
        userTag = new JLabel("Username:");
        GridBagConstraints gbc_userTag = new GridBagConstraints();
        gbc_userTag.insets = new Insets(0, 0, 0, 5);
        gbc_userTag.anchor = GridBagConstraints.EAST;
        gbc_userTag.gridx = 0;//Here are your x + y coords
        gbc_userTag.gridy = 1;//Adding to x moves left, adding to y moves down
        panel.add(userTag, gbc_userTag);

        //Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
          //textField = new JTextField();
        txtUserName = new JTextField();
        GridBagConstraints grdUserName = new GridBagConstraints();
        grdUserName.fill = GridBagConstraints.HORIZONTAL;
        grdUserName.gridx = 1;
        grdUserName.gridy = 1;
        txtUserName.setColumns(10);
        panel.add(txtUserName, grdUserName);
        //panel.add(textField,txtUserName);
        //textField.setColumns(10);

    JLabel balTag = new JLabel("Current Balance:");
        GridBagConstraints gbc_balTag = new GridBagConstraints();
        gbc_balTag.insets = new Insets(0, 0, 0, 5);
        gbc_balTag.anchor = GridBagConstraints.EAST;
        gbc_balTag.gridx = 0;  
        gbc_balTag.gridy = 0;
        panel.add(balTag, gbc_balTag);

        lblBalTag = new JLabel("[________________]");
        lblBalTag.setToolTipText("Balance as of the last call to the peerbet site.");
        GridBagConstraints gbc_lblBalTag = new GridBagConstraints();
        gbc_lblBalTag.insets = new Insets(0, 0, 0, 5);
        gbc_lblBalTag.anchor = GridBagConstraints.EAST;
        gbc_lblBalTag.gridx = 1;
        gbc_lblBalTag.gridy = 0;
        panel.add(lblBalTag, gbc_lblBalTag);

        JLabel startTag = new JLabel("Starting Bid:");
        GridBagConstraints gbc_startTag = new GridBagConstraints();
        gbc_startTag.insets = new Insets(0, 0, 0, 5);
        gbc_startTag.anchor = GridBagConstraints.EAST;
        gbc_startTag.gridx = 0;
        gbc_startTag.gridy = 3;
        panel.add(startTag, gbc_startTag);

        txtStartBid = new JTextField();
        GridBagConstraints grdStartBid = new GridBagConstraints();
        grdStartBid.fill = GridBagConstraints.HORIZONTAL;
        grdStartBid.gridx = 1;
        grdStartBid.gridy = 3;
        txtStartBid.setText("0.00000010");
        txtStartBid.setEnabled(false);
        panel.add(txtStartBid, grdStartBid);

        JLabel multTag = new JLabel("Multiplier:");
        GridBagConstraints gbc_multTag = new GridBagConstraints();
        gbc_multTag.insets = new Insets(0, 0, 0, 5);
        gbc_multTag.anchor = GridBagConstraints.EAST;
        gbc_multTag.gridx = 0;
        gbc_multTag.gridy = 4;
        panel.add(multTag, gbc_multTag);

        txtMultiplier = new JTextField();
        GridBagConstraints grdMultiplier = new GridBagConstraints();
        grdMultiplier.fill = GridBagConstraints.HORIZONTAL;
        grdMultiplier.gridx = 1;
        grdMultiplier.gridy = 4;
        txtMultiplier.setColumns(10);
        txtMultiplier.setText("2");
        txtMultiplier.setEnabled(false);
        panel.add(txtMultiplier, grdMultiplier);

        JLabel minTag = new JLabel("Min Remaining:");
        GridBagConstraints gbc_minTag = new GridBagConstraints();
        gbc_minTag.insets = new Insets(0, 0, 0, 5);
        gbc_minTag.anchor = GridBagConstraints.EAST;
        gbc_minTag.gridx = 0;
        gbc_minTag.gridy = 5;
        panel.add(minTag, gbc_minTag);

        txtMinRemaining = new JTextField();
        GridBagConstraints grdMinRemaining = new GridBagConstraints();
        grdMinRemaining.fill = GridBagConstraints.HORIZONTAL;
        grdMinRemaining.gridx = 1;
        grdMinRemaining.gridy = 5;
        txtMinRemaining.setColumns(10);
        txtMinRemaining.setText("0");
        txtMinRemaining.setEnabled(false);
        panel.add(txtMinRemaining, grdMinRemaining);

        txtPassword = new JPasswordField();
        GridBagConstraints grdPassword = new GridBagConstraints();
        grdPassword.fill = GridBagConstraints.HORIZONTAL;
        grdPassword.gridx = 1;
        grdPassword.gridy = 2;
        txtPassword.setEchoChar('*');
        txtPassword.setColumns(10);
        panel.add(txtPassword, grdPassword);

        passTag = new JLabel("Password:");
        GridBagConstraints gbc_passTag = new GridBagConstraints();
        gbc_passTag.insets = new Insets(0, 0, 0, 5);
        gbc_passTag.anchor = GridBagConstraints.EAST;
        gbc_passTag.gridx = 0;
        gbc_passTag.gridy = 2;
        panel.add(passTag, gbc_passTag);

        txtOdds = new JTextField();
        GridBagConstraints grdOdds = new GridBagConstraints();
        grdOdds.fill = GridBagConstraints.HORIZONTAL;
        grdOdds.gridx = 1;
        grdOdds.gridy = 6;
        txtOdds.setColumns(10);
        txtOdds.addActionListener(this);
        txtOdds.setText("49.5");
        txtOdds.setEnabled(false);
        panel.add(txtOdds, grdOdds);

        JLabel oddsTag = new JLabel("Odds %:");
        GridBagConstraints gbc_oddsTag = new GridBagConstraints();
        gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
        gbc_oddsTag.anchor = GridBagConstraints.EAST;
        gbc_oddsTag.gridx = 0;
        gbc_oddsTag.gridy = 6;
        panel.add(oddsTag, gbc_oddsTag);

        txtMaxBet = new JTextField();
        GridBagConstraints grdMaxBet = new GridBagConstraints();
        grdMaxBet.fill = GridBagConstraints.HORIZONTAL;
        grdMaxBet.gridx = 1;
        grdMaxBet.gridy = 7;
        txtMaxBet.setColumns(10);
        txtMaxBet.setText("1");
        txtMaxBet.setEnabled(false);
        panel.add(txtMaxBet, grdMaxBet);

        //This is the Combo Box
        cmbCurrency = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
        GridBagConstraints gbc_list = new GridBagConstraints();
        gbc_list.fill = GridBagConstraints.HORIZONTAL;
        gbc_list.gridx = 1;
        gbc_list.gridy = 9;
        cmbCurrency.addActionListener(this);
        cmbCurrency.setEnabled(false);
        panel.add(cmbCurrency, gbc_list);

        JLabel maxTag = new JLabel("MaxBet:");
        GridBagConstraints gbc_maxTag = new GridBagConstraints();
        gbc_maxTag.insets = new Insets(0, 0, 0, 5);
        gbc_maxTag.anchor = GridBagConstraints.EAST;
        gbc_maxTag.gridx = 0;
        gbc_maxTag.gridy = 7;
        panel.add(maxTag, gbc_maxTag);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.SOUTH);
        panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        txtRollAmnt = new JTextField();
        GridBagConstraints grdRollAmnt = new GridBagConstraints();
        grdRollAmnt.fill = GridBagConstraints.HORIZONTAL;
        grdRollAmnt.gridx = 1;
        grdRollAmnt.gridy = 8;
        txtRollAmnt.setColumns(10);
        txtRollAmnt.setText("0");
        //txtRollAmnt.setEnabled(false);
        panel.add(txtRollAmnt, grdRollAmnt);

        RollAmntCheck = new JCheckBox("Quit on win after:");    
        RollAmntCheck.setSelected(true);
        GridBagConstraints grdRollAmntCheck = new GridBagConstraints();
        grdRollAmntCheck.fill = GridBagConstraints.HORIZONTAL;
        grdRollAmntCheck.gridx = 0;
        grdRollAmntCheck.gridy = 8;
    panel.add(RollAmntCheck, grdRollAmntCheck);

        AlwaysOnTopCheck = new JCheckBox("Always On Top");  
        AlwaysOnTopCheck.setSelected(false);
        AlwaysOnTopCheck.addActionListener(this);
        GridBagConstraints grdAlwaysOnTopCheck = new GridBagConstraints();
        grdAlwaysOnTopCheck.fill = GridBagConstraints.HORIZONTAL;
        grdAlwaysOnTopCheck.gridx = 0;
        grdAlwaysOnTopCheck.gridy = 9;
        menu.add(AlwaysOnTopCheck, grdAlwaysOnTopCheck);

        btnDonate = new JButton("Login");
        btnDonate.addActionListener(this);
        panel_1.add(btnDonate);

        btnHigh = new JButton("Roll High");
        btnHigh.addActionListener(this);
        btnHigh.setEnabled(false);
        panel_1.add(btnHigh);

        btnLow = new JButton("Roll Low");
        btnLow.addActionListener(this);
        btnLow.setEnabled(false);
        panel_1.add(btnLow);

        btnFloat = new JButton("Roll Float");
        btnFloat.addActionListener(this);
        btnFloat.setEnabled(false);
        btnFloat.setVisible(false);
        panel_1.add(btnFloat);

        btnClearLog = new JButton("Clear Log");
        btnClearLog.addActionListener(this);
        panel_1.add(btnClearLog);

    btnCalc = new JButton("Max Loss Streak");
        btnCalc.addActionListener(this);
    btnCalc.setEnabled(false);
        btnCalc.setVisible(true);
        panel_1.add(btnCalc);

        scrollCheck = new JCheckBox("Auto-Scroll"); 
        scrollCheck.setSelected(true);
        menu.add(scrollCheck);

        scrollDisable = new JCheckBox("Disable Log");   
        scrollDisable.setSelected(false);
        menu.add(scrollDisable);

        btnCalc.setToolTipText("Click here to calculate the max loss streak you can handle based on current imputs.");
        btnClearLog.setToolTipText("Click here to clear the log!");
        btnHigh.setToolTipText("Click here to Roll High!");
        btnLow.setToolTipText("Click here to Roll Low!");
        btnFloat.setToolTipText("Click here to Roll?");
        scrollCheck.setToolTipText("Toggles the auto-scroll function of the log.");
        txtMaxBet.setToolTipText("The dicebot will not bet above amount entered in.");
        txtOdds.setToolTipText("What odds(%) will the dicebot be rolling?");
        txtPassword.setToolTipText("Enter your peerbet account password.");
        AlwaysOnTopCheck.setToolTipText("Click to have the bot always appear above other windows");
        txtMinRemaining.setToolTipText("The bot will stop when account has less than this amount in bank.");
        txtMultiplier.setToolTipText("What shall the bet be multiplied by upon loss?");
        txtStartBid.setToolTipText("What amount should the bot start each bet at?");
        txtUserName.setToolTipText("Enter your peerbet account username.");
        lblBalTag.setToolTipText("Current amount of chosen currency shown here.");
        cmbCurrency.setToolTipText("Choose the currency that the bot will be using to roll with.");
        txtRollAmnt.setToolTipText("Number of times to roll before stopping. 0 = Infinite");
        RollAmntCheck.setToolTipText("Select to stop only after a win. Unselect to stop as soon as the # of rolls is reached");

        JScrollPane scrollPane = new JScrollPane();//   JScrollPane scrollPanel1 = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
        contentPane.add(scrollPane, BorderLayout.CENTER);
        txtInfo = new JTextArea("All number formats must use a period(.)\n");
        txtInfo.setColumns(35);
        txtInfo.setEnabled(false);
        txtInfo.setDisabledTextColor(Color.black);
        scrollPane.setViewportView(txtInfo);

        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == cmbCurrency) {
            if (cmbCurrency.getSelectedIndex() == 0){
                txtStartBid.setText("0.00000010");
            }else{
                txtStartBid.setText("0.0001");
            }
            if(APIKey != null){
                String balance = peerbetapi.get_balance(dicebot.APIKey);
                JsonObject jsonObject = JsonObject.readFrom(balance);
                if(jsonObject.get("status").asInt() == 1){
                    lblBalTag.setText(jsonObject.get("raffle_cur" + Integer.toString((cmbCurrency.getSelectedIndex() + 10))).asString());
                }
            }else{
                lblBalTag.setText("[________________]");
            }
        }else if (e.getSource() == btnLow){
            if(btnLow.getText() == "Roll Low"){
                btnHigh.setText("Stop");
                btnLow.setText("Stop On Win");
                btnFloat.setEnabled(false);
                Dicebotcode dbc = new Dicebotcode();
                Dicebotcode.RollType = "low";
                Dicebotcode.StopRollingOnWin = false;
                Dicebotcode.StopRolling = false;
                dbc.dbc();
            }else{
                // The EnableAllFields function will re-enable the buttons once its done.
                btnLow.setText("Waiting...");
                btnLow.setEnabled(false);
                Dicebotcode.StopRollingOnWin = true;
            }
        }else if (e.getSource() == btnHigh){
            if(btnHigh.getText() == "Roll High"){
                btnHigh.setText("Stop");
                btnLow.setText("Stop On Win");
                btnFloat.setEnabled(false);
                Dicebotcode dbc = new Dicebotcode();
                Dicebotcode.RollType = "high";
                Dicebotcode.StopRollingOnWin = false;
                Dicebotcode.StopRolling = false;
                dbc.dbc();
            }else{
                // The EnableAllFields function will re-enable the buttons once its done.
                btnHigh.setText("Stopping...");
                btnHigh.setEnabled(false);
                btnLow.setEnabled(false);
                Dicebotcode.StopRolling = true;
            }
        }else if (e.getSource() == btnFloat){
            if(btnFloat.getText() == "Roll Float"){
                btnHigh.setText("Stop");
                btnLow.setText("Stop On Win");
                btnFloat.setEnabled(false);
                Dicebotcode dbc = new Dicebotcode();
                Dicebotcode.RollType = "float";
                Dicebotcode.StopRollingOnWin = false;
                Dicebotcode.StopRolling = false;
                dbc.dbc();
            }else{
                // The EnableAllFields function will re-enable the buttons once its done.
                btnFloat.setText("Stopping...");
                btnFloat.setEnabled(false);
                Dicebotcode.StopRolling = true;
            }
        }else if (e.getSource() == btnClearLog){
            txtInfo.setText("");
        }else if (e.getSource() == btnDonate){
            //donate d = new donate();
            if(btnDonate.getText() == "Login"){
                String reply = null;
                try {
                    reply = peerbetapi.login(txtUserName.getText(), String.copyValueOf(txtPassword.getPassword()));
                } catch (IOException e1) {
                    reply = "{\"status\":0, \"message\":\"An unknown error has occurred while attempting to login.\"}";
                }
                JsonObject json = JsonObject.readFrom(reply);
                if(json.get("status").asInt() != 1){
                    txtInfo.append("Error: " + json.get("message").asString() + "\n");
                    txtInfo.setCaretPosition(txtInfo.getText().length());
                }else{
                    APIKey = json.get("key").asString();
                    lblBalTag.setText(json.get("raffle_cur" + Integer.toString(cmbCurrency.getSelectedIndex() + 10)).asString());
                    btnDonate.setText("Donate");
                    userTag.setVisible(false);
                    txtUserName.setVisible(false);
                    passTag.setVisible(false);
                    txtPassword.setVisible(false);
                    txtStartBid.setEnabled(true);
                    txtMultiplier.setEnabled(true);
                    txtMinRemaining.setEnabled(true);
                    txtOdds.setEnabled(true);
                    txtMaxBet.setEnabled(true);
                    cmbCurrency.setEnabled(true);
                    btnHigh.setEnabled(true);
                    btnLow.setEnabled(true);
                    btnFloat.setEnabled(true);
                    txtInfo.append("Login successful!\n");
                    txtInfo.setCaretPosition(txtInfo.getText().length());
                }
            }else{
                donate.showdonate();
            }
        }else if(e.getSource() == AlwaysOnTopCheck){
            if(AlwaysOnTopCheck.isSelected() == true){
                frame.setAlwaysOnTop(true);
            }else{
                frame.setAlwaysOnTop(false);
            }
        }
    }
}
疯狂程序员

您永远不会设置框架菜单栏,例如...

frame.setJMenuBar(menuBar);

另外,static不是您的朋友。如果您发现自己需要填写字段static,则可能是您做错了什么

相反,内构建UI的main方法,我想创建,其职责是建立用户界面和显示主窗口中的“主”类,这意味着你可以得到出static的上下文main方法

您可以看一看,主要方法invokeLater的创建方式是否不同于Java主要方法中通常的创建方式?举个例子

更新

两件事情...

首先,请遵循已给出的建议,不要将添加JMenuBar到内容窗格中,而是将其设置为JFrames菜单栏...

menuBar = new JMenuBar();
//contentPane.add(menuBar);
setJMenuBar(menuBar);

其次,不要在菜单栏中添加组件,这不是它的工作方式

//menu.add(AlwaysOnTopCheck, grdAlwaysOnTopCheck);

花点时间通读如何使用菜单,以更好地了解它们的工作方式

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尽管设置为框架,但 Java 菜单栏未出现在 Swing GUI 中

来自分类Dev

菜单栏未出现在Tkinter GUI上

来自分类Dev

PyQt4菜单栏未出现在Ubuntu中

来自分类Dev

菜单未出现在片段中

来自分类Dev

菜单栏出现在iframe后面

来自分类Dev

配置后,菜单栏不会出现在tkinter中

来自分类Dev

如何停止出现在菜单栏和附件中的窗口控件

来自分类Dev

如何创建出现在顶部菜单栏中的桌面应用程序?

来自分类Dev

在某些设备上,“选项菜单”未出现在操作栏中

来自分类Dev

在某些设备上,“选项菜单”未出现在操作栏中

来自分类Dev

如何避免“ Data Lake”菜单项出现在Visual Studio 2015的菜单栏中?

来自分类Dev

菜单项未出现在导航抽屉中

来自分类Dev

iPhone未出现在Safari的“开发”菜单中

来自分类Dev

该菜单未出现在地图活动中

来自分类Dev

OBIEE创建代理未出现在菜单中

来自分类Dev

新菜单项未出现在 UI 中

来自分类Dev

子类别未出现在 wordpress 管理菜单中

来自分类Dev

导航栏未出现在Xamarin的DetailPage中

来自分类Dev

JMenu 栏未出现在 GUI 中

来自分类Dev

工具栏未出现在 UWP 中

来自分类Dev

Nautilus菜单出现在透明的gnome-shell菜单栏的后面

来自分类Dev

Nautilus菜单出现在透明的gnome-shell菜单栏的后面

来自分类Dev

Java Enterprise未出现在IntelliJ中

来自分类Dev

滚动条未出现在 Java Swing 中

来自分类Dev

按钮未出现在JFrame中

来自分类Dev

MBean未出现在JConsole中

来自分类Dev

模态未出现在刷新中

来自分类Dev

图例未出现在ggplot中

来自分类Dev

标题未出现在Firefox中