小编kle*_*tra的帖子

我想通过单击添加按钮动态添加JLabel和文本框

当我动态创建文本和标签框时,它应该采用"Textbox:Labelbox"的格式,然后当我再次点击添加按钮时,相同的模式应该在下一行重复,依此类推......我应该使用哪种布局以及如何使用?

这是我使用的代码

if(field_name.getText().equals("")){  
            error.setForeground(Color.red);    
            error.setText("Enter the Field name first");
        } else {  
        JLabel l = new JLabel(field_name.getText(), JLabel.RIGHT);   
        JTextField textField = new JTextField();  
        Dimension dim = new Dimension(20,30);  
        textField.setPreferredSize(dim);  
        field_layer.add(l);  
        field_layer.add(textField);  
        SpringUtilities.makeCompactGrid(field_layer,  
                                numPairs, 2, //rows, cols  
                                6, 6,        //initX, initY  
                                6, 6);       //xPad, yPad  
        numPairs++;  
        field_layer.invalidate();  
        this.pack();  
        }  
Run Code Online (Sandbox Code Playgroud)

java layout swing

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

通过JPA绑定Java JTable

我试图寻找合适的答案,但到目前为止没有任何帮助.我对java GUI编程很新,实际上,对于java本身.但是我有经理要了解JPA,如何使用JPA检索,插入和删除.

现在我希望我的数据库中的数据显示在JTable中.

我目前有以下mySQL表(我希望在JTable中显示)

游戏Id PK int Title Publisher Genre ReleaseDate

至于编码问题,我使用以下方法成功检索了表中包含的数据:

public List<Game> getGames(){
    List<Game> games;

    try{
        TypedQuery<Game> selectGamesQuery = entityManager.createQuery("SELECT g FROM Game g", Game.class);
        games = selectGamesQuery.getResultList();
    } catch(Exception e) {
        System.out.println(e);
    } 
    return games;
}
Run Code Online (Sandbox Code Playgroud)

这成功地返回了我可以迭代的游戏列表.

然后,在我看来,我有以下几点

        JScrollPane scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.CENTER);

    tblGames = new JTable(new tblGamesModel());
    tblGames.setShowVerticalLines(true);
    tblGames.setShowHorizontalLines(true);
    tblGames.setFillsViewportHeight(true);
    scrollPane.setViewportView(tblGames);
Run Code Online (Sandbox Code Playgroud)

哪一个导致我们进入桌面模型,这就是我被困住的地方.

public class tblGamesModel extends AbstractTableModel {

private GameRepository gameRepository;
private List<Game> games;
/**
 * 
 */
public tblGamesModel(){
    gameRepository = new GameRepository();
    games …
Run Code Online (Sandbox Code Playgroud)

java swing jpa jtable populate

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

JTextField中的数字数组 - Java

我正在尝试让JTextField接收一个数字数组.我尝试使用"for"以及运行数组所需的所有东西,但没有成功.这是一段代码.

private void cbxGeraValorActionPerformed(java.awt.event.ActionEvent evt) 
{        
    if(cbxRandom.isSelected())
    {
        double[] num = new double[10];

        for(int i=0; i<10; i++)
        {
            Random r = new Random();
            num[i] = r.rnd;
            txtValor.setText(String.valueOf(num[i]));
        }
    }
Run Code Online (Sandbox Code Playgroud)

java arrays swing jtextfield

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

JTextArea仅包含数字,但允许负值

我有一个JTextArea,只需要接受数字.这是我的代码:

DocumentFilter onlyNumberFilter = new AxisJTextFilter();
    final JTextArea areaTextoXMin = new JTextArea(String.valueOf(xMin));
    ((AbstractDocument)areaTextoXMin.getDocument()).setDocumentFilter(onlyNumberFilter);
Run Code Online (Sandbox Code Playgroud)

适用于正数,但不适用于负数.我该如何解决这个问题?

编辑:对不起,AxisJTextFilter是在互联网上找到的,我忘记了.它的代码是:

import javax.swing.text.*;
import java.util.regex.*;

public class AxisJTextFilter extends DocumentFilter
{
    public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
        sb.insert(offset, text);
        if(!containsOnlyNumbers(sb.toString())) return;
        fb.insertString(offset, text, attr);
    }
    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
        sb.replace(offset, offset + length, text); …
Run Code Online (Sandbox Code Playgroud)

java swing jtextarea negative-number documentfilter

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

如何使用jgoodies表单最大化组件高度

Noob问题:我有以下表单布局(请原谅JRuby语法).我希望所有三个按钮都能将其高度拉伸以填充可用空间.但只有按钮3才这样做.

require 'java'
require './lib/jgoodies-common-1.2.1.jar'
require './lib/jgoodies-forms-1.4.2.jar'

java_import javax.swing.JButton
java_import javax.swing.JFrame

java_import com.jgoodies.forms.layout.CellConstraints
java_import com.jgoodies.forms.layout.FormLayout

class Foo < JFrame
  def initialize
    super
    cc = CellConstraints.new

    layout = FormLayout.new(
      "10dlu, pref:grow, 10dlu, pref:grow, 10dlu",
      "10dlu, pref:grow, 10dlu, pref:grow, 10dlu"
    )
    layout.setRowGroups([[2, 4]])
    layout.setColumnGroups([[2, 4]])

    self.setLayout(layout)

    self.add(JButton.new("button 1"), cc.xy(2, 2))
    self.add(JButton.new("button 2"), cc.xy(2, 4))
    self.add(JButton.new("button 3"), cc.xywh(4, 2, 1, 3))

    self.pack
    self.setVisible(true)
    self.toFront
  end
end

Foo.new
Run Code Online (Sandbox Code Playgroud)

提示和指示赞赏.

--Ben

java layout swing jgoodies

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

在JAVA中将时间字段H:M转换为整数字段(分钟)

JTable包括时间字段,例如"01:50".我需要将此值读入整数变量.为此,我想将时间转换为分钟.例如,"01:50"应转换为110.

要解决此任务,首先我将时间值保存为String.

String minutS = tableModel.getValueAt(1,1).toString();
Run Code Online (Sandbox Code Playgroud)

其次,我将处理此String并提取符号前后的整数:.最后,我将计算总分钟数.这个解决方案有效吗?也许,有可能用日历代替它或者像这样吗?

java time swing

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

actionPerformed处的NullPointerException.不知道为什么

我正在创建一个使用GUI来显示抵押付款的Java程序.我正在尝试向textArea输出将用于抵押的付款.这是我的代码:

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

public class MortgageGui extends JFrame implements ActionListener {

// Set two-places for decimal format
DecimalFormat twoPlaces = new DecimalFormat("$0.00");

// Declare variable for calculation
Double loanAmt;
double interestRate;
double monthlyPayment;
int payment;
String amount;

JTextField loanAmount;
JComboBox loanTypeBox;
JLabel paymentOutput;
JTextArea paymentList;

// Build arrays for mortgages
double[] mortgage1 = {7.0, 5.35, 0.0}; // (years, interest, monthly payment)
double[] mortgage2 = {15.0, 5.5, 0.0}; // (years, interest, monthly payment)
double[] mortgage3 …
Run Code Online (Sandbox Code Playgroud)

java swing nullpointerexception

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

检测JTextArea中的文本选择

我有一个JTextArea并且我正在检测是否有任何文本是选择,如果没有,那么两个菜单项是灰色的.我遇到的问题是,当我编译并打开应用程序时,我必须首先单击JTextArea,然后菜单项显示为灰色,如果我不这样做,即使没有选择文本也不是.我正在使用以下插入符号监听器.

    textArea.addCaretListener(new CaretListener() {

        @Override
        public void caretUpdate(CaretEvent arg0) {
            int dot = arg0.getDot();
            int mark = arg0.getMark();
            if (dot == mark) {

                copy2.setEnabled(false);
                cut1.setEnabled(false);
            }
            else{
                cut1.setEnabled(true);
                copy2.setEnabled(true);
            }

        }
    });
Run Code Online (Sandbox Code Playgroud)

java user-interface swing caret jtextarea

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

谜题:这个片段有什么问题

......它是如何发生的(猜测允许,当然:-)

@Override
public int convertChildIndexToModel(int modelIndex) {
    return getRowSorter() != null ? getRowSorter()
            .convertRowIndexToView(modelIndex) : modelIndex;
}

@Override
public int convertChildIndexToView(int viewIndex) {
    return getRowSorter() != null ? getRowSorter()
            .convertRowIndexToModel(viewIndex) : viewIndex;
}
Run Code Online (Sandbox Code Playgroud)

是的,白痴是我;-)

java swing jtable tablerowsorter

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

仅允许 JTextfield 中的数字

我正在尝试实现以下要求:

JTextield 中只接受数字,当按下非数字键时,将不被接受。

我尝试了很多东西,甚至尝试调用退格事件来删除最后一个字符(如果它是非数字)。但是,无法删除在文本字段中键入的值。我试图理解 DOCUMENT FILTER 但发现它很难实现。如果有人帮助我解决问题,我会很高兴。

java swing jtextfield

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