小编Hov*_*els的帖子

如何将JTextField限制为ax个字符数

我必须限制JTextField中的字符数.我使用以下代码来做到这一点,但问题是我使用虚拟键盘将数据提供给JTextField.因此偏移量始终设置为0.当我输入超过指定数量的字符时,它会重置字段并从头开始执行.例如,如果我的限制是3个字符并且我进入xyz0我的有限文本框,则读取字符z,然后清除该字段并重新启动.所以我留0在了现场.代码如下.

  public class JTextFieldLimit extends PlainDocument {
  private int limit;  
  public JTextFieldLimit(int limit) {  
   super();  
   this.limit = limit;  
   }  
    @Override  
  public void insertString( int offset, String  str, AttributeSet attr ) throws   BadLocationException {  
    if (str == null) return;  
            System.out.println("from document helper getLength():"+getLength());  
            System.out.println("from document helper str.length():"+str.length());  
            System.out.println("from document helper str:"+str);  
            System.out.println("from document helper attr:"+attr);  
            System.out.println("from document helper offset:"+offset);  
    if ((getLength() + str.length()) <= limit) {  
      super.insertString(offset, str, attr);  
    }  
  }  
}  
Run Code Online (Sandbox Code Playgroud)

java swing jtextfield

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

如何在Java中聆听按键时移动图像.

我开始学习java编程,我认为通过游戏开发学习java很酷.我知道如何绘制图像并听取按键然后移动该图像.但是当窗口正在聆听按键时,是否可以使图像前后移动到窗口?例如,当图像或物体(如宇宙飞船)在窗口中从左向右移动时,如果我按空格键,激光将在屏幕底部闪光(酷吧:D).但基本上我只想知道如何在窗口正在聆听按键时让图像从左向右移动.

我想我会在我的窗口添加一个关键监听器,然后触发一个无限循环来移动图像.或者我是否需要了解线程以便另一个线程移动对象?

请指教.

非常感谢.

java animation swing event-handling

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

Java Swing JtextField插图

我正在使用Netbeans GUI,我想在jTextField的开头添加3个像素的空间:

在此输入图像描述

我已经在GUI中尝试了setMargin,setInset,但它没有改变任何东西.

我有另一个问题,为什么右下边框不圆?这是我的代码:

Border roundedBorder = new LineBorder(new Color(210,210,210), 1, true);
researchTextField.setBorder(roundedBorder);
Run Code Online (Sandbox Code Playgroud)

非常感谢你,

问候

java swing border margin jtextfield

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

关于 ImageIcons 的方法不起作用

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Cards extends JFrame {

private GridLayout grid1;

    JButton []bt=new JButton[52];

    ImageIcon tail=new ImageIcon(getClass().getResource("b1fv.png"));

    ImageIcon ori;

    public Cards(){
        grid1=new GridLayout(7,9,2,2);
        setLayout(grid1);
        for(int i=0;i<bt.length;i++){
            ImageIcon c=new ImageIcon(getClass().getResource(i+1+".png"));
            bt[i]=new JButton(c);
            bt[i].addActionListener(new RatingMouseListener(i));
            add( bt[i]);
        }
    }

    public static void main(String[] args){
        Cards frame=new Cards();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1400,700);
        frame.setVisible(true);
    }

    private class RatingMouseListener implements ActionListener {
        private  int index=0;


        public RatingMouseListener(int index) {
            this.index = index;

        }




        public void actionPerformed(ActionEvent e) {
              System.out.println("Mouse entered for rating " …
Run Code Online (Sandbox Code Playgroud)

java swing actionlistener imageicon

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

循环使用java中的信用卡验证

我是计算机科学入门课程的高中生.我们的任务如下:

信用卡号码的最后一位是校验位,可防止转录错误,例如单个数字错误或切换两位数字.以下方法用于验证实际的信用卡号,但为简单起见,我们将为8位数而不是16位的数字描述它:

  • 从最右边的数字开始,形成每个其他数字的总和.例如,如果信用卡号是4358 9795,那么您形成总和5 + 7 + 8 + 3 = 23.
  • 将前一步骤中未包含的每个数字加倍.添加结果数字的所有数字.例如,使用上面给出的数字,将数字加倍,从倒数第二个开始,得到18 18 10 8.在这些值中添加所有数字得到1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.
  • 添加前两个步骤的总和.如果结果的最后一位为0,则该数字有效.在我们的例子中,23 + 27 = 50,所以这个数字是有效的.

编写实现此算法的程序.用户应提供一个8位数字,您应该打印出该号码是否有效.如果它无效,您应该打印出使数字有效的校验位的值.

我做了一切,除了粗体部分.我的代码如下:

public class CreditCard 
{ 

    private String creditCardNumber;
    private boolean valid;
    private int checkDigit;
    int totalSum;

    /**
     * Constructor for objects of class CreditCard
     */
    public CreditCard(String pCreditCardNumber)
    {
        creditCardNumber = pCreditCardNumber;
        checkDigit = Integer.parseInt(pCreditCardNumber.substring(creditCardNumber.length() - 1));
        int sumOfDigits = checkDigit + …
Run Code Online (Sandbox Code Playgroud)

java loops if-statement

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

如何正确调整自定义组件的大小?

所以我有一个自定义按钮,运行良好,没有错误.

这是代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class LukeButton extends JComponent implements MouseListener{
    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setTitle("Luke");
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        LukeButton lb = new LukeButton();
        lb.addActionListener(e->{
            System.out.println("Success");
        });
        frame.add(lb, BorderLayout.CENTER);

        frame.setVisible(true);
    }
//ArrayList of listeners
private final ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();

public LukeButton(){
    this.addMouseListener(this);
}
//Adds a listeners to the list
public void addActionListener(ActionListener e){
    listeners.add(e);
}
//Called when button is …
Run Code Online (Sandbox Code Playgroud)

java swing

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

如何避免嵌套的ActionListeners?

在我的程序中,我希望用户:

  1. 选择/打开一个数据库(如Access)自己
  2. 从数据库中选择一个表
  3. 从表中选择列

在我的代码中,我有一个类,它做这样的事情:

mntmOpenDatabase.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //open the database
        //display tables as buttons
        tableButton.addActionListener(new ActionListener() { // select a table
            public void actionPerformed(ActionEvent e) {
                //display the columns of the table selected as buttons
                    colButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {// add to the list of columns to be exported }
Run Code Online (Sandbox Code Playgroud)

这导致了一个非常大的代码块.有更清洁,更简单的方法吗?

java user-interface swing

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

图像未在 React 中加载

无法将图像显示为未找到错误,但我提供了它的完整路径。我不知道我哪里出错了。

class App extends React.Component {

   render() {
      return (
               <div>
                  <h1> hello</h1>    
                <img src="/home/priyanka/Finalproject/src/components/3.jpg" alt="cannot display"/>    
              </div>    
           );
         }
  }

export default App;
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

Helm upgrade --install 没有接受新的变化

我在我的构建 CI 中使用下面的命令,以便在每次构建时部署掌舵。但是,我注意到未部署更改。

              helm upgrade --install --force \
              --namespace=default \
              --values=kubernetes/values.yaml \
              --set image.tag=latest \
              --set service.name=my-service \
              --set image.pullPolicy=Always \
              myService kubernetes/myservice
Run Code Online (Sandbox Code Playgroud)

我每次都需要标记图像吗?如果存在相同版本,helm 是否不进行安装?

kubernetes-helm

5
推荐指数
3
解决办法
5410
查看次数

getText()vs getPassword()

我正在为一家虚拟公司设计一个登录系统,现在我所拥有的只是主要登录,需要大量的清理工作.下面是我的登录处理程序.

private class LoginButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if(_uid.getText().equalsIgnoreCase("Nathan") && _pwd.getText().equals("password")) {
            JOptionPane.showMessageDialog(null, "Congratulations on logging in!");
        } else {
          JOptionPane.showMessageDialog(null, "Error on login!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

原样,这种方法非常好,但是当我改为它时

_pwd.getPassword.equals("password")
Run Code Online (Sandbox Code Playgroud)

当一切输入正确时,它会直接指向else语句.这有什么不对?完整的程序如下.

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

public class Main extends JFrame {
    private static final int HEIGHT = 90;
    private static final int WIDTH = 400;

    JTextField _uid = new JTextField(10);
    JPasswordField _pwd = new JPasswordField(10);
    JButton _login = new JButton("Login");
    JButton _reset = new JButton("Reset"); …
Run Code Online (Sandbox Code Playgroud)

java passwords swing jpasswordfield

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