小编alm*_*OSU的帖子

如何在网格布局中设置按钮的位置?

我的代码:

public class Form {
    public static void main(String[] args) {
        Form form = new Form();
        form.go();
    }

    public void go() {
        JFrame form = new JFrame();
        GridLayout layout = new GridLayout(2,7);
        Label nameLabel = new Label("Name");
        form.setLayout(layout);
        JTextField nameBox = new JTextField();
        form.getContentPane().add(nameLabel);
        form.getContentPane().add(nameBox);
        form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        form.setSize(500,500);
        form.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我如何设置JTextField的这个位置,使其为2,7而不是1,2?

java swing grid-layout

2
推荐指数
1
解决办法
3万
查看次数

Javafx - 组合框文本字段bg颜色

我用Google搜索并搜索了,但不幸的是,我没有找到如何通过css 使不可编辑的组合框包含带有白色背景文本字段的方法.

换句话说,如何使不可编辑的组合框看起来像可编辑的组合框(即相同的焦点,箭头按钮等)?

代码:

.combo-box .text-field{
    -fx-background-color: white;
}
Run Code Online (Sandbox Code Playgroud)

不起作用.

有人可以帮忙吗?

java javafx

2
推荐指数
1
解决办法
2428
查看次数

使用==比较字符串

可以编写如下所示的代码.

问题:即使我输入正确的员工ID,它也总是说"无效的员工ID".
请告诉我为什么以及如何正确地做到这一点.

#include <iostream>
#include <iomanip>
using namespace std;
char select, js;
char empid[4];
double bSalary, bonus, tot=0.0;
int main()
{
    do
    {
        cout<<"Employee id: ";
        cin>>empid;
        if(empid=="M001" || empid=="A004" || empid == "M002") //these are employee ids
        {   
            cout<<"Job Status: ";
            cin>>js;
            if(js=='P' || js=='C')
            {
                cout<<"Basic Salary: ";
                cin>>bSalary;
                if(bSalary>75000 && js=='P')
                {
                    bonus = bSalary*(20.0/100.0);
                    tot = tot + bonus + bSalary;
                }
                else if(bSalary>75000 && js=='C')
                {
                    bonus = bSalary*(15.0/100.0);
                    tot = tot + …
Run Code Online (Sandbox Code Playgroud)

c++ string if-statement

2
推荐指数
1
解决办法
117
查看次数

JCheckBox没有出现

我已经检查了无处可修复但没有任何方法可以使我的复选框出现.我将它添加到面板并将面板添加到窗口.按钮出现所以它必须是复选框的问题.这是我的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainApplication {

    public static Toolkit tk = Toolkit.getDefaultToolkit();

    public static void main(String[] args) {
        MainApplication instance = new MainApplication();
        instance.start();
    }

    private JFrame window;
    private JPanel mainPanel;
    private JPanel contingencyPanel;

    private JButton applyButton = new JButton("Apply Changes");
    private JCheckBox autoRedLightBox = new JCheckBox("Red Light");
    private JCheckBox autoYellowLightBox = new JCheckBox("Yellow Light");
    private JCheckBox autoGreenLightBox = new JCheckBox("Green Light");
    private …
Run Code Online (Sandbox Code Playgroud)

java swing layout-manager jcheckbox

2
推荐指数
1
解决办法
1077
查看次数

当我尝试查找刚刚放入hashmap的值时,为什么会出现Runtime异常?

有人可以向我解释为什么以下代码段会抛出运行时异常吗?

int i = 0;
for ( ; i < TOTAL; i++ ) {
    String value = "" + i;
    int key = numbers[i];                
    map.put(key, value);
}

i = 0;
for ( ; i < TOTAL; i++ ) {
    String value = "" + i;
    int key = numbers[i];
    String valueInMap = map.get(key);
    if(valueInMap == null || !valueInMap.equals(value)) {
        throw new RuntimeException("Impossible!");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是整个程序,除了我numbers[]用随机数填充的部分.

我指的是throw new RuntimeException("Impossible!");我得到这个例外

java string hashmap

2
推荐指数
1
解决办法
53
查看次数

java循环输出中不需要的重复

我正在编写一个代码来重塑信号.我得到了不必要的重复输出.

INPUT:

String [] rani = {"A","1","2","OK","B","3","4","OK","B","1","3", "好"};

要求的输出:

A/3 B/7 B/4

得到了输出:

A/3 A/3 A/3 A/3 B/7 B/7 B/7 B/7 B/4


算法:单字母串("A","B"等)后面跟着数字串("1","2"等).每个字母表字符串后跟斜杠和数字的总和,字符串"OK"将被忽略.

作为java和编程的新手,我需要帮助才能获得所需的输出.

我的代码是:

public class SignalOK {

    public static void main(String[] arg) {
        String finalSignal = "";
        String netSignal = "";
        String name = "";
        int total = 0;
        String[] rani = { "A", "1", "2", "OK", "B", "3", "4", "OK", "B", "1",
                "3", "OK" };

        for (int i = 0; i < rani.length; i++) {

            if ((rani[i] …
Run Code Online (Sandbox Code Playgroud)

java arrays string loops

2
推荐指数
1
解决办法
54
查看次数

如何向 JFrame Gui 添加按钮

我正在尝试向框架 gui 添加一个按钮。
我尝试制作一个面板并将其添加到其中,但它不起作用。请帮忙!

这是我的代码:

import javax.swing.*;

public class Agui extends JFrame {
    public Agui() {
        setTitle("My Gui");
        setSize(400, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton button;
        JPanel panel;

        // my error lines are under the "panel" and  "button"
        // it says i must implement the variables. what does that mean???
        panel.add(button);
    }
    public static void main(String[] args) {
        Agui a = new Agui();
    }
}
Run Code Online (Sandbox Code Playgroud)

java swing button jframe

2
推荐指数
1
解决办法
4万
查看次数

JAVA和+运算符的字符串类

请检查我的代码并回答,告诉我它在java技术中是如何工作的.

String s1 = "roushan8";
String s2 = "roushan"+8;
String s3 = "roushan"+s1.length();
System.out.println(s1==s2);
System.out.println(s2==s3);
System.out.println(s3==s1);
Run Code Online (Sandbox Code Playgroud)

结果是:

true
false
false
Run Code Online (Sandbox Code Playgroud)

java

2
推荐指数
1
解决办法
91
查看次数

HashMap Entry类的recordAccess()方法是空体

API有这样的评论..

但是它做了什么?既然它有空身方法.

/**
 * This method is invoked whenever the value in an entry is
 * overwritten by an invocation of put(k,v) for a key k that's already
 * in the HashMap.
 */
void recordAccess(HashMap<K,V> m) {
}
Run Code Online (Sandbox Code Playgroud)

java collections hashmap

1
推荐指数
1
解决办法
520
查看次数

线程主java.util.InputMismatchException中的异常

我正在实现一个简单的HashMap程序,它存储人名和年龄.这是我的代码:

import java.util.*;

class StoreName {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            String name = sc.nextLine();
            int age = sc.nextInt();
            map.put(name, age);
        }

        for (String key : map.keySet())
            System.out.println(key + "=" + map.get(key));
    }
}
Run Code Online (Sandbox Code Playgroud)

当我从nextInt()获取输入时,Scanner会抛出InputMismatchException异常,但如果我从nextLine()获取输入然后将其解析为int,那么我的代码就会正常运行.请解释一下.

如果我可以将字符串输入解析为任何类型,为什么我应该使用nextInt()或nextDouble().

java

1
推荐指数
1
解决办法
380
查看次数