小编kle*_*tra的帖子

单击更改JButton的颜色将不起作用

public class LevelEditorButton extends JButton
{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private int i;

public int getState()   {return i;}
public void increaseState()
{
    if(i == 2)
        i = 0;
    else
        i++;
    changeState();
}

public LevelEditorButton()
{
    i = 0;
    changeState();
    this.setOpaque(true);
}
public void changeState()
{
    if(i == 0)
        this.setBackground(Color.GREEN);
    else if(i == 1)
        this.setBackground(Color.RED);
    else 
        this.setBackground(Color.BLACK);
    this.setOpaque(true);
}
}

public class ChangeColorButtonListener extends LevelEditorButton implements ActionListener
{
     @Override
     public void actionPerformed(ActionEvent ae) 
    {
    this.increaseState(); …
Run Code Online (Sandbox Code Playgroud)

java swing

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

java中的自定义对话框

我试图使用以下代码构建自定义对话框.

public class CustomDialog
{

    JTextField firstName;
    JButton jb;
    public CustomDialog() 
    {
        firstName=new JTextField();
        jb= new JButton("Browse");
        final JComponent[] inputs = new JComponent[] 
        {
               new JLabel("SDK Path"),firstName,jb
        };
        JOptionPane.showMessageDialog(null, inputs, 
                "Enter the SDK Path", JOptionPane.PLAIN_MESSAGE);   
    }
Run Code Online (Sandbox Code Playgroud)

此代码的结果是textfield后跟按钮,然后以垂直方式OK.我想在OK按钮旁边有一个按钮.我怎样才能做到这一点??

如何将OK Command的标题更改为"Save"?

请帮我解决这个问题.

java swing dialog joptionpane

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

如何在Java中使用变量?

在我的空闲时间里,我一直在学习Java,到目前为止,我对如何正确使用变量一无所知.我的意思是,我应该更喜欢使用实例变量,还是只使用单个方法中的变量.

例如,这是我写的一段代码:

public class arsenalTroop {
    String[][] troopStats;
String[][] weaponStats;
String[][] armorStats;
String[][] animalStats;
String[] troops;
String[] weapon;
String[] armor;
String[] animal;
JLabel[] troopsArray;
int troopTotal;
int weaponTotal;
int armorTotal;
int lordTotal;
int animalTotal;

JFrame arsenalLordFrame = new JFrame();
JTextField name = new JTextField();
JLayeredPane lP = new JLayeredPane();
JLayeredPane fP = new JLayeredPane();
JLayeredPane ldP= new JLayeredPane();
JLabel siegeLabel = new JLabel();
JComboBox weaponCB;
JComboBox armorCB;
JComboBox animalCB;
JLabel siegeText = new JLabel();
JButton addWep = new JButton();
JButton …
Run Code Online (Sandbox Code Playgroud)

java variables swing coding-style instance-variables

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

刷新JComboBox

我有一个组合框,你可以在代码中看到从表中获取值.所以在我点击确定后,表格的值会发生变化.如何在不关闭和打开jframe的情况下看到compobox的这些新值?我今天做了很多关于java.awt.EventQueue.invokeLater和其他工作人员的研究,但我不能使它工作,我是java新手和编程通用.所以这是代码:

public class Compo extends JFrame implements ActionListener
{//start of class Compo 
//start of variables
private JComboBox<String> CompoBox;
private String array[];
private JButton okButton;
private JPanel panel;
//end of variables
public Compo ()
{//start of Compo method
  super("Example"); 
    panel=new JPanel(null);

   //table =  new String[3];
   array= new String[3];
   array[0]="alpha";
   array[1]="beta";
   array[2]="charlie";

   CompoBox= new JComboBox<>(array);
   CompoBox.setBounds(50, 70, 100, 20);
   panel.add(CompoBox);
   okButton=new JButton("ok");
   okButton.setBounds(50, 120, 70, 30);
   okButton.setActionCommand("ok");
   okButton.addActionListener(this);
   panel.add(okButton);
   add(panel);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setSize(200, 200);
   setVisible(true);


}//end of compo method
@Override
public void actionPerformed(ActionEvent …
Run Code Online (Sandbox Code Playgroud)

java swing jcombobox

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

使用JFileChooser保存

我使用的是JFileChoosershowSaveDialoge()setSelectionMode(JfileChooser.DIRECTORIES_ONLY)设置在那里预选文件将被保存,它会被调用.

我希望用户能够选择新版本的名称以及放置它的位置.我该怎么做?我还想选择一个默认名称.

java swing jfilechooser file save

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

jFileChooser作为表格中的单元格编辑器

我想设置一个jFileChooser作为表的单个单元格的编辑器(不是该表的整个列,因为将使用各种其他编辑器,如comboBox等).有任何建议或示例代码吗?(我已经在这里查看了这些示例如何使用Oracle的表

java swing jfilechooser jtable

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

textFields上的无限焦点循环

我有2个JTextFields:

JTextField txtJobType, txtPriorityCode;
Run Code Online (Sandbox Code Playgroud)

这是我需要的功能:

当用户在txtJobType中键入"管理"并点击选项卡(或点击远离)时,将执行错误检查以查看该字段是否为空或输入的文本是否存在于数据库中.我这样做的方式是:

private void txtJobTypeFocusLost(java.awt.event.FocusEvent evt) {                                     
    System.out.println("JobType Focus Lost");
    if (!checkFieldExists(txtJobType.getText(), "jobType", "jobCode",
            JobType.class) || txtJobType.getText().isEmpty()) {
        txtJobType.requestFocusInWindow();
        txtJobType.selectAll();
    } else {
    }
} 
Run Code Online (Sandbox Code Playgroud)

因此,如果该字段不存在或文本为空,则将焦点返回到txtJobType并突出显示所有文本(如果有)

这没有问题.但是,我有txtPriorityCode字段需要具有完全相同的行为.所以我做了:

private void txtPriorityCodeFocusLost(java.awt.event.FocusEvent evt) {                                          
    System.out.println("PriorityCode Focus Lost");
    if (!checkFieldExists(txtPriorityCode.getText(), "priority", "priorityCode",
            Priority.class) || txtPriorityCode.getText().isEmpty()) {
        txtPriorityCode.requestFocusInWindow();
        txtPriorityCode.selectAll();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是问题开始的地方:如果用户将jobType和制表符保留为优先级,则代码会尝试将焦点返回到jobtype,但由于此时优先级也为空,它将尝试从jobtype返回焦点,从而产生此输出:

PriorityCode Focus Lost
JobType Focus Lost
PriorityCode Focus Lost
JobType Focus Lost
Run Code Online (Sandbox Code Playgroud)

任何有关如何实现此行为的帮助表示赞赏,因为我必须为至少10个其他文本字段执行此操作.

谢谢!

java swing focus error-checking infinite-loop

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

安装JTable来填充JPanel

嗨,这是我的表格设置的代码:

String [] column = {"MacAddress","PcName","OperatingSystem","IpAddress","Port","Status"};
    model = new DefaultTableModel(0,column.length);
    model.setColumnIdentifiers(column);
    mainTable = new JTable(model);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for(int i=0;i<=column.length-1;i++){
    mainTable.getColumnModel().getColumn(i).setPreferredWidth(300);
    }
    pane = new JScrollPane(mainTable);
    pnlTabel = new JPanel();
    pnlTabel.setBorder(BorderFactory.createTitledBorder(""));
    pnlTabel.setPreferredSize(new Dimension(dim.width*70/100, dim.height*60/100));
    pnlTabel.add(pane);
addMainPanel(pnlTabel);
Run Code Online (Sandbox Code Playgroud)

这是我的addMainPanel()函数:

public void addMainPanel(Component pnl){
    mainPanel.add(pnl);
    mainPanel.revalidate();
}
Run Code Online (Sandbox Code Playgroud)

这是我的mainPanel的代码:

    mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
add(mainPanel,"Center");
Run Code Online (Sandbox Code Playgroud)

我正在为我的框架使用边框布局:

setLayout(new BorderLayout(0,0));
Run Code Online (Sandbox Code Playgroud)

我的问题是,即使我使用这组代码来设置我的JTable以适应但似乎所有这一切都失败了,这段代码:

 mainTable.setAutoResizeMode(JTa![enter image description here][1]ble.AUTO_RESIZE_OFF);
for(int i=0;i<=column.length-1;i++){
mainTable.getColumnModel().getColumn(i).setPreferredWidth(300);
}
Run Code Online (Sandbox Code Playgroud)

何时使用该代码,我的jtable不会调整大小,只会添加到底部的水平滚动条上.

java swing jtable

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

如何检查是否在buttongroup中选择了radiobutton?

我有一组5个单选按钮,我需要检查是否至少选择了其中一个.

这是正确的吗?

buttonGroup1.isSelected(ButtonModel);

但这是什么ButtonmModel

java swing netbeans-7

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

在JScrollPane中获取正确的鼠标位置

这是我的问题:

在此输入图像描述

我正在创建一个下拉菜单,当右键单击JTree中的一个元素时,问题是JTree在JScrollPane中,并且窗格越滚动,弹出菜单就越远了.

右键单击会触发一个MouseEvent,它会在以下代码中被拦截,其结果是创建一个新的弹出菜单.

@Override
public void mouseClicked(MouseEvent e) {
    if (SwingUtilities.isRightMouseButton(e)) {

        int row = tree.getClosestRowForLocation(e.getX(), e.getY());
        tree.setSelectionRow(row);

        TreePath path = tree.getPathForRow(row);  

        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();  

        // If this object is a Search...
        if(node.getUserObject().getClass() == Search.class) {
            jp = new ItemEditPopUpMenu(tree, row, true);
        } else {
            jp = new ItemEditPopUpMenu(tree, row, false);
        }

        jp.show(this, e.getX(), e.getY());
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在做的就是从MouseEvent抓取位置并将其用作在以下行中创建弹出菜单的位置:

jp.show(this, e.getX(), e.getY());
Run Code Online (Sandbox Code Playgroud)

现在我注意到这是发送事件的元素的相对位置,这解释了问题,JScrollPanel在滚动时从JFrame偏移,问题是如何判断到多远?

我只是不知道,这就是我需要帮助的地方.非常感谢提前!

java swing jscrollpane

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