JTextField txt=new JTextField( columns: 20);

时间: 2023-12-06 07:42:48 浏览: 165
这段代码创建了一个文本框对象,它有 20 个列可见。 具体来说,JTextField 是 Swing 组件库中的一个类,用于创建单行文本框。在这里,我们使用构造函数 JTextField(int columns) 来创建文本框对象,其中参数 columns 指定了文本框可见的列数。在这个例子中,文本框可见的列数是 20。
相关问题

public class InsertJPanel extends JPanel implements ActionListener { private JLabel[] jLabel=new JLabel[4]; private String[] s={"学 号:","姓 名:","年 龄:","专 业:"}; private JTextField[] jTextField=new JTextField[4]; private JButton addJButton=new JButton("添加"); public InsertJPanel() { setLayout(new MigLayout( "hidemode 3", // columns "[fill]" + "[fill]" + "[fill]" + "[fill]" + "[fill]", // rows "[]" + "[]" + "[]" + "[]" + "[]" + "[]")); for (int i=0;i<jLabel.length;i++){ jLabel[i]=new JLabel(s[i]); this.add(jLabel[i],"cell 2 "+(i+1)); jTextField[i]=new JTextField(20); this.add(jTextField[i],"cell 4 "+(i+1)); this.add(new JLabel( )); } add(addJButton, "cell 2 5 3 1"); addJButton.addActionListener(this); }

这段代码定义了一个名为 InsertJPanel 的类,该类继承自 JPanel,并实现了 ActionListener 接口,用于处理添加按钮的点击事件。该类中包含了四个 JLabel 和四个 JTextField 组件,用于输入学生的学号、姓名、年龄和专业信息,以及一个添加按钮。在构造方法中,使用 MigLayout 布局管理器设置了该面板的布局和组件的位置和大小。具体来说,将该面板设置为 6 行 5 列的网格布局,并使用 "hidemode 3" 参数表示隐藏模式为“不自动”,表示在组件隐藏时不会影响布局。然后,使用 for 循环创建并添加四个 JLabel 和 JTextField 组件,以及一个空的 JLabel 组件,将它们分别放在第 2 和第 4 列的不同行上。最后,将添加按钮添加到第 5 行,第 2 列到第 5 列的单元格中,并为其添加 ActionListener 监听器,以便在按钮点击时执行添加操作。

public updateDialog(Frame owner) { super(owner, "查找" , true); that=owner; setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setBounds(100, 100, 270, 354);//设置了对话框的大小和位置 getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(new BorderLayout(0, 0)); { JPanel panel = new JPanel(); contentPanel.add(panel, BorderLayout.WEST); panel.setLayout(new GridLayout(0, 1, 0, 0)); { JLabel label = new JLabel("编号:"); panel.add(label); } { JLabel label = new JLabel("身份证号:"); panel.add(label); } { JLabel label = new JLabel("药品信息:"); panel.add(label); } { JLabel label = new JLabel("价格:"); panel.add(label); } { JLabel label = new JLabel("状态:"); panel.add(label); } } { JPanel panel = new JPanel(); contentPanel.add(panel, BorderLayout.CENTER); panel.setLayout(new GridLayout(0, 1, 0, 0)); { textField_1 = new JTextField(); panel.add(textField_1); textField_1.setColumns(10); } { textField_2 = new JTextField(); panel.add(textField_2); textField_2.setColumns(10); } { textField_3 = new JTextField(); panel.add(textField_3); textField_3.setColumns(10); } { textField_4 = new JTextField(); panel.add(textField_4); textField_4.setColumns(10); } { textField_5 = new JTextField(); panel.add(textField_5); textField_5.setColumns(10); } }

这段代码是一个 Java 类中的构造函数,用于创建一个名为 "查找" 的对话框。该对话框包含了一些标签和文本框,用于输入编号、身份证号、药品信息、价格和状态等信息。 对话框的大小和位置通过 `setBounds` 方法进行设置,布局使用了边界布局(BorderLayout)。对话框的内容面板(contentPanel)的边框也进行了设置。 内容面板中包含了两个面板,分别是位于 WEST 和 CENTER 的面板。WEST 面板使用了网格布局(GridLayout),包含了编号、身份证号、药品信息、价格和状态等标签。CENTER 面板也使用了网格布局,包含了对应的文本框。 每个标签和文本框都被添加到了相应的面板中,并设置了适当的大小和位置。 这段代码还设置了对话框的默认关闭操作为 DISPOSE_ON_CLOSE,表示关闭对话框时只会将其隐藏而不销毁。
阅读全文

相关推荐

import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.*; import javax.swing.JFileChooser; public class demo extends JFrame{ public static void main(String[] args) { JFrame frame=new JFrame();//创建一个窗口 frame.setTitle("开始你的复制吧!");//设置窗口标题 frame.setBounds(400,400,400,300);//设置窗口的位置和大小 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口关闭时的操作 frame.setBackground(Color.CYAN);//设置Frame的背景色 frame.setLayout(null);//不适用布局管理器,设置为NULL JTextField textField1=new JTextField(30); JTextField textField2=new JTextField(30); JLabel label1=new JLabel("输入你要拷贝的文件:"); JLabel label2=new JLabel("输入拷到哪里去:"); textField1.setColumns(30); textField2.setColumns(30); label1.setBounds(10,10,120,20); label2.setBounds(10,40,120,20); textField1.setBounds(140,10,200,20); textField2.setBounds(140,40,200,20); frame.add(label1); frame.add(textField1); frame.add(label2); frame.add(textField2); //frame.setSize(400,100); //frame.setLocation(300,200); frame.setVisible(true); JButton button1 =new JButton("copy"); button1.setBounds(270,70,100,20); button1.setBackground(Color.CYAN);//做作的天蓝色 frame.add(button1); JButton button2=new JButton("..."); button2.setBounds(350,12,20,16); frame.add(button2); JButton button3=new JButton("..."); button3.setBounds(350,42,20,16); frame.add(button3); JFileChooser fileChooser=new JFileChooser("d://"); fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//可以选择文件和文件夹 fileChooser.showOpenDialog(null);//文件打开对话框 } }怎么把文件选择器和button2联系起来,达到点击button2就会弹出文件选择器的效果

public Calculator() { super("Calculator"); this.setLocationRelativeTo(null);// set up window this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(600, 200); result = new JPanel(); answer = 0.0 ; show = new JLabel(Double.toString(answer), SwingConstants.RIGHT);// let the result at the right of the field result.add(show); JTextField input = new JTextField(Double.toString(answer));// setup the size of the text field input.setColumns(6); input.setHorizontalAlignment(JTextField.RIGHT); button = new JPanel(); button.setLayout(new GridLayout(1, 5)); CLR = new JButton("CLR");// add the buttons of calculation. add = new JButton("+"); minus = new JButton("-"); Multiply = new JButton("*"); divide = new JButton("/"); button.add(CLR); button.add(add); button.add(minus); button.add(Multiply); button.add(divide); CLR.addActionListener(e -> {// clear answer = 0.0; show.setText(Double.toString(answer)); field.setText(" "); }); add.addActionListener(e -> {// add number = field.getText(); if (!number.equals(null)) { double value = Double.parseDouble(number); answer += value; show.setText(Double.toString(answer)); field.setText(""); } }); minus.addActionListener(e -> {// minus number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer -= value; show.setText(Double.toString(answer)); field.setText(""); } }); Multiply.addActionListener(e -> {// multiply number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer *= value; show.setText(Double.toString(answer)); field.setText(""); } }); divide.addActionListener(e -> {// divide number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer /= value; show.setText(Double.toString(answer)); field.setText(""); } }); add(button); add(field); add(result); this.setLayout(new GridLayout(3, 1)); this.setVisible(true); }错哪了

public class Calculator extends JFrame { private JPanel result, button; private JLabel show; private JButton CLR, add, minus, Multiply, divide; private JTextField field; private Double answer; private String number; public Calculator() { super("Calculator"); this.setLocationRelativeTo(null);// set up window this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(600, 200); result = new JPanel(); answer = 0.0 ; show = new JLabel(Double.toString(answer), SwingConstants.RIGHT);// let the result at the right of the field result.add(show); JTextField filed = new JTextField(Double.toString(answer));// setup the size of the text field filed.setColumns(6); filed.setHorizontalAlignment(JTextField.RIGHT); button = new JPanel(); button.setLayout(new GridLayout(1, 5)); CLR = new JButton("CLR");// add the buttons of calculation. add = new JButton("+"); minus = new JButton("-"); Multiply = new JButton("*"); divide = new JButton("/"); button.add(CLR); button.add(add); button.add(minus); button.add(Multiply); button.add(divide); CLR.addActionListener(e -> {// clear answer = 0.0; show.setText(Double.toString(answer)); field.setText(" "); }); add.addActionListener(e -> {// add number = field.getText(); if (!number.equals(null)) { double value = Double.parseDouble(number); answer += value; show.setText(Double.toString(answer)); field.setText(""); } }); minus.addActionListener(e -> {// minus number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer -= value; show.setText(Double.toString(answer)); field.setText(""); } }); Multiply.addActionListener(e -> {// multiply number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer *= value; show.setText(Double.toString(answer)); field.setText(""); } }); divide.addActionListener(e -> {// divide number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer /= value; show.setText(Double.toString(answer)); field.setText(""); } }); add(button); add(field); add(result); this.setLayout(new GridLayout(3, 1)); this.setVisible(true); }

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); lblNewLabel = new JLabel("请输入一个成语:"); lblNewLabel.setBounds(10, 10, 150, 30); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel); textField = new JTextField(); textField.setBounds(160, 10, 200, 30); textField.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(textField); textField.setColumns(10); lblNewLabel_1 = new JLabel("当前成语:"); lblNewLabel_1.setBounds(10, 50, 150, 30); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel_1); lblNewLabel_2 = new JLabel(currentIdiom); lblNewLabel_2.setBounds(160, 50, 200, 30); lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel_2); btnNewButton = new JButton("提交"); btnNewButton.setBounds(160, 90, 100, 30); btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); lblNewLabel = new JLabel("请输入一个成语:"); lblNewLabel.setBounds(10, 10, 150, 30); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel); textField = new JTextField(); textField.setBounds(160, 10, 200, 30); textField.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(textField); textField.setColumns(10); lblNewLabel_1 = new JLabel("当前成语:"); lblNewLabel_1.setBounds(10, 50, 150, 30); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel_1); lblNewLabel_2 = new JLabel(currentIdiom); lblNewLabel_2.setBounds(160, 50, 200, 30); lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel_2); btnNewButton = new JButton("提交"); btnNewButton.setBounds(160, 90, 100, 30); btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));

package 作业5; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.io.File; import java.util.HashMap; import java.util.Scanner; import java.io.File; import java.util.HashMap; import java.util.Scanner; public class Example13_7{ public static void main( String args []){ WindowWord win = new WindowWord(); win.setTitle("英﹣汉小字典"); } } class WindowWord extends JFrame{ JTextField inputText,showText; WordPolice police; public WindowWord(){ setLayout(new FlowLayout()); inputText = new JTextField(6); showText = new JTextField(6); showText.setColumns(15); add(inputText); add(showText); police = new WordPolice(); police.setView(this); inputText.addActionListener(police); setBounds(100,100,400,280); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class WordPolice implements ActionListener { WindowWord view; HashMap<String,String>hashtable; File file = new File ("E:\\JAVA\\上机\\作业5\\cet4.txt"); Scanner sc = null ; WordPolice () { hashtable = new HashMap< String,String >(); //System.out.println(hashtable.size()); try { sc = new Scanner ( file ); //System.out.println(sc); while (sc.hasNext ()) { String englishWord = sc . next (); String chineseWord = sc . next (); hashtable . put ( englishWord , chineseWord ); } HashMap<String,String> reversedHashtable = new HashMap<String,String>(); for (String key : hashtable.keySet()) { String value = hashtable.get(key); reversedHashtable.put(value, key); } hashtable.putAll(reversedHashtable); } catch(Exception e ) {} } public void setView(WindowWord view) { this.view = view; } public void actionPerformed(ActionEvent e) { String inputWord = view.inputText.getText (); if(hashtable.containsKey(inputWord)) { String outputWord = hashtable.get ( inputWord ); view.showText.setText (outputWord ); } else { String chineseWord = inputWord; for (String key : hashtable.keySet()) { String value = hashtable.get(key); if (value.equals(chineseWord)) { view.showText.setText(key); return; } } view.showText.setText ("没有此单词"); } } }把这段代码完善一下,让它能实现汉语翻译为英语,英语也能翻译为汉语,GUI界面不变

public AddCourseFrm() { setClosable(true); setIconifiable(true); setTitle("\u6DFB\u52A0\u8BFE\u7A0B"); setBounds(100, 100, 453, 471); JLabel label = new JLabel("\u8BFE\u7A0B\u540D\u79F0\uFF1A"); label.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/images/\u8BFE\u7A0B.png"))); label.setFont(new Font("微软雅黑", Font.PLAIN, 14)); courseNameTextField = new JTextField(); courseNameTextField.setColumns(10); JLabel label_1 = new JLabel("\u6388\u8BFE\u8001\u5E08\uFF1A"); label_1.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/images/\u8001\u5E08.png"))); label_1.setFont(new Font("微软雅黑", Font.PLAIN, 14)); teacherListComboBox = new JComboBox(); JLabel label_2 = new JLabel("\u5B66\u751F\u4EBA\u6570\uFF1A"); label_2.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/images/\u4EBA\u6570.png"))); label_2.setFont(new Font("微软雅黑", Font.PLAIN, 14)); studentNumTextField = new JTextField(); studentNumTextField.setColumns(10); JLabel label_3 = new JLabel("\u8BFE\u7A0B\u4ECB\u7ECD\uFF1A"); label_3.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/images/\u4ECB\u7ECD.png"))); label_3.setFont(new Font("微软雅黑", Font.PLAIN, 14)); courseInfoTextArea = new JTextArea(); JButton addCourseButton = new JButton("\u786E\u8BA4\u6DFB\u52A0"); addCourseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { addCourseAct(ae); } }); addCourseButton.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/images/\u786E\u8BA4.png"))); addCourseButton.setFont(new Font("微软雅黑", Font.PLAIN, 14)); JButton resetButton = new JButton("\u91CD\u7F6E\u4FE1\u606F"); resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { resetValue(ae); } });说明每句代码作用

package 作业5; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.; import java.io.File; import java.util.HashMap; import java.util.Scanner; import java.io.File; import java.util.HashMap; import java.util.Scanner; public class Example13_7{ public static void main( String args []){ WindowWord win = new WindowWord(); win.setTitle("英﹣汉小字典"); } } class WindowWord extends JFrame{ JTextField inputText,showText; WordPolice police; public WindowWord(){ setLayout(new FlowLayout()); inputText = new JTextField(6); showText = new JTextField(6); showText.setColumns(15); add(inputText); add(showText); police = new WordPolice(); police.setView(this); inputText.addActionListener(police); setBounds(100,100,400,280); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class WordPolice implements ActionListener { WindowWord view; HashMap<String,String>hashtable; File file = new File ("E:\JAVA\上机\作业5\cet4.txt"); Scanner sc = null ; WordPolice () { hashtable = new HashMap< String,String >(); //System.out.println(hashtable.size()); try { sc = new Scanner ( file ); //System.out.println(sc); while (sc.hasNext ()) { String englishWord = sc . next (); String chineseWord = sc . next (); hashtable . put ( englishWord , chineseWord ); } HashMap<String,String> reversedHashtable = new HashMap<String,String>(); for (String key : hashtable.keySet()) { String value = hashtable.get(key); reversedHashtable.put(value, key); } hashtable.putAll(reversedHashtable); } catch(Exception e ) {} } public void setView(WindowWord view) { this.view = view; } public void actionPerformed(ActionEvent e) { String inputWord = view.inputText.getText (); if(hashtable.containsKey(inputWord)) { String outputWord = hashtable.get ( inputWord ); view.showText.setText (outputWord ); } else { String chineseWord = inputWord; for (String key : hashtable.keySet()) { String value = hashtable.get(key); if (value.equals(chineseWord)) { view.showText.setText(key); return; } } for (String value : hashtable.values()) { if (value.equals(inputWord)) { String outputWord = hashtable.get(value); view.showText.setText(outputWord); return; } } view.showText.setText ("没有此单词"); } } }我在输入框输入“书”,但结果是没有此单词,但是输入“book”,结果是“n.书,书籍”,如何解决