- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于某些基于 GUI 的功能的几个问题。
问题1:
将 jComboBox(制造商和车辆)中的数据显示到 jTextArea 中。在下面的代码中,我使用插入函数设置文本区域第一行的文本。但我希望它在顶部显示制造商,然后显示用户选择的车辆。
private void ManufacturersActionPerformed(java.awt.event.ActionEvent evt) {
String str1 = (String) Manufacturers.getSelectedItem();
jTextArea1.insert(str1, WIDTH);
}
private void VehiclesActionPerformed(java.awt.event.ActionEvent evt) {
String str2 = (String) Vehicles.getSelectedItem();
jTextArea1.insert(str2, WIDTH);
我想我可以将其更改为 setText 函数或其他函数。
问题2:
我有 2 个 jlist,我想将用户选择的多个数据显示到 jTextArea 中,但我不知道要使用从 NetBeans 函数中选择的事件处理程序。
下面的程序代码:
package rlc.building.program;
/*
*
* @author Jacob
*/
public class GUI extends javax.swing.JFrame {
/**
* Creates new form GUI
*/
public GUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
Modifications = new javax.swing.JList();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Parts = new javax.swing.JList();
Reset = new javax.swing.JButton();
Manufacturers = new javax.swing.JComboBox();
Vehicles = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Modifications.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "LED Light Bar £250", "Rally Light Hood Mount £150", "Rally Lights x2 £100", "Limo Tinted Windows £1500", "Custom Exo-Skelton Roll Cage £3000", "Custom Roll-Cage £1000", "Thick White Wall Tires £1450", "Front and Rear Strut Bars £400", " ", " " };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane2.setViewportView(Modifications);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane3.setViewportView(jTextArea1);
jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
jLabel1.setText("Real Life Car Building Program");
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel2.setText("Modifications");
jLabel2.setToolTipText("This area below is a list of all the modifications you can choose from for any vehicle.");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel3.setText("Manufacturers");
jLabel3.setToolTipText("The car companys that you can choose from.");
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel4.setText("Vehicles");
jLabel4.setToolTipText("The vehicles you can choose from.");
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel5.setText("Parts");
jLabel5.setToolTipText("The parts that you can install on the vehicle straight from the manufacturer.");
Parts.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Leather Seats £1000", "New Stainless Steel Exhaust £500", "Alloy Wheels £1200", "Checker Plated Steel Panels £400", " " };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(Parts);
Reset.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
Reset.setText("Reset");
Reset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResetActionPerformed(evt);
}
});
Manufacturers.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chevrolet", "Dodge", "Ford", "Honda", "Infiniti", "Land Rover", "Mazda", "Mitsubishi", "Nissan", "Pontiac", "Scion", "Subaru", "Toyota", "Volkswagen" }));
Manufacturers.setToolTipText("This is the drop down menu that lists the manufacturers chosen. Click the down arrow to see the selection.");
Manufacturers.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ManufacturersActionPerformed(evt);
}
});
Vehicles.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1957 Bel Air", "1970 El Camino SS 454", "2014 Corvette Stingray", "1970 Camaro Z28", "1996 Impala SS", "2014 Super Sport", "2015 Chevrolet Corvette Z06", " ", "2015 Hellcat Charger R/T ", "2015 Hellcat Challenger ", "2015 Dodge Viper ", "2015 Dodge Dart ", "1969 Dodge Charger R/T ", "1969 Dodge Charger Daytona Hemi", " ", "1956 Ford F100 ", "1987 Ford Sierra Cosworth RS500 ", "1973 Ford XB Falcon GT ", "1977 Ford Escort RS1800 ", "2015 Ford Mustang GT ", "1973 Ford Capri RS3100 ", "1975 Ford Bronco ", "2003 Ford Focus RS ", "2014 Ford Fiesta ST", " ", "1997 Honda Civic Type R ", "2009 Honda S2000 CR ", "1994 Honda Civic 1.5 VTi ", "1999 Honda Civic Si Coupe ", "1991 Honda CR-X SiR ", "2000 Honda Integra Type-R ", "1986 Honda Civic Si", " ", "2003 Infiniti G35 ", "2006 Infiniti G37 ", "2015 Infiniti Q60 Coupe ", "2015 Infiniti Q70 ", "2015 Infiniti QX50", " ", "2015 Land Rover The Defender ", "2015 Land Rover Discovery ", "2015 Land Rover Range Rover", " ", "1990 Mazda MX-5 Miata ", "2016 Mazda MX-5", "1997 Mazda RX-7 ", "1994 Mazda MX-5 Miata ", "2007 Mazda MX-5 Roadster Coupe ", "1990 Mazda Savanna RX-7 ", "2011 Mazda RX-8 R3 ", "1985 Mazda RX-7 GSL-SE ", "1973 Mazda RX-3", " ", "2008 Mitsubishi Lancer Evolution X GSR ", "1999 Mitsubishi Lancer Evolution VI GSR ", "2006 Mitsubishi Lancer Evolution IX MR ", "2010 Mitsubishi Colt Ralliart ", "1988 Mitsubishi Starion ESI-R ", "2004 Mitsubishi Lancer Evo VIII MR ", "1992 Mitsubishi Galant VR-4 ", "2015 Mitsubishi Shogun ", "2015 Mitsubishi L200", " ", "2010 Nissan 370Z ", "1994 Nissan 240SX SE ", "1971 Nissan Skyline 2000GT-R", "2002 Nissan Skyline GT-R V-Spec II ", "2010 Nissan GT-R SpecV ", "2000 Nissan Silvia Spec-R ", "2012 Nissan GT-R Black Edition ", "1992 Nissan Silvia CLUB K's ", "2015 Nissan IDx NISMO", " ", "1969 Pontiac GTO Judge ", "1973 Pontiac Firebird Trans Am SD-455 ", "1977 Pontiac Firebird Trans Am ", "1969 Pontiac Firebird Trans Am ", "1965 Pontiac GTO ", "2006 Pontiac GTO", " ", "2013 Scion FR-S ", "2012 Scion tC ", "2014 xB ", "2015 Scion FR-S ", "2015 Scion tC", " ", "2011 Subaru WRX STI ", "1990 Subaru Legacy RS ", "1998 Subaru Impreza 22B STi ", "2004 Subaru Impreza WRX STi ", "2005 Subaru Impreza WRX STI ", "2008 Subaru Impreza WRX STi ", "2013 Subaru BRZ ", "1980 Subaru BRAT GL ", "2015 Forester", " ", "2015 Toyota Land Cruiser V8 ", "2015 Toyota Hilux ", "2015 Toyota Prius ", "1985 Toyota Sprinter Trueno GT Apex ", "1969 Toyota 2000GT ", "1974 Toyota Celica GT ", "1998 Toyota Supra RZ ", "2013 Toyota GT86", " ", "1963 Volkswagen Beetle ", "1963 Volkswagen Type 2 De Luxe", "2003 Volkswagen Golf R32 ", "2010 Volkswagen Golf R ", "2011 Volkswagen Polo GTI ", "2015 VolksWagen Up! ", "2015 VolksWagen New Scirocco ", "2015 VolksWagen e-up!", " ", " " }));
Vehicles.setToolTipText("");
Vehicles.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
VehiclesActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(208, 208, 208)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(59, 59, 59)
.addComponent(jLabel3)
.addGap(163, 163, 163)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(93, 93, 93))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3)
.addGap(18, 18, 18)
.addComponent(Reset)
.addGap(26, 26, 26))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(71, 71, 71)
.addComponent(jLabel4))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Manufacturers, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Vehicles, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jLabel3)))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(Manufacturers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Vehicles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(Reset)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
.addContainerGap())))
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))))
);
Manufacturers.getAccessibleContext().setAccessibleName("Manufacturers");
Vehicles.getAccessibleContext().setAccessibleName("Vehicles");
pack();
}// </editor-fold>
private void ResetActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText("");
}
private void ManufacturersActionPerformed(java.awt.event.ActionEvent evt) {
String str1 = (String) Manufacturers.getSelectedItem();
jTextArea1.insert(str1, WIDTH);
}
private void VehiclesActionPerformed(java.awt.event.ActionEvent evt) {
String str2 = (String) Vehicles.getSelectedItem();
jTextArea1.insert(str2, WIDTH);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox Manufacturers;
private javax.swing.JList Modifications;
private javax.swing.JList Parts;
private javax.swing.JButton Reset;
private javax.swing.JComboBox Vehicles;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
感谢您的帮助,雅各布。
最佳答案
1) setText()
将立即设置整个文本。在您的情况下,使用 append()
会更方便,它“将给定的文本附加到文档的末尾。”
2) 您应该在 JList
上使用 addListSelectionListener()
,以便“每次选择发生更改时都收到通知”。然后您可以更新 JTextArea
中的文本。
例如
myJList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
myTextArea.setText(""); // Reset the text area, to avoid appending multiples times the same values
for(Object o : myJList.getSelectedValuesList()){
myTextArea.append(o.toString()+"\n");
}
}
});
编辑:由于您有多种来源将数据写入文本区域,因此您应该将所有写入集中在一种方法中:
public void updateTextArea(){
myTextArea.setText(""); // Reset the text area, to avoid appending multiples times the same values
// Write from comboboxes
myTextArea.append( (String) Manufacturers.getSelectedItem() );
myTextArea.append( (String) Vehicles.getSelectedItem());
// Write from the JList
for(Object o : myJList.getSelectedValuesList()){
myTextArea.append(o.toString()+"\n");
}
}
并从所有监听器中调用该方法:
private void ManufacturersActionPerformed(java.awt.event.ActionEvent evt) {
updateTextArea();
}
private void VehiclesActionPerformed(java.awt.event.ActionEvent evt) {
updateTextArea();
}
...
myJList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
updateTextArea();
}
});
关于java - 如何将 JComboBoxes 和 JLists 中的信息显示到 JTextArea/JTextfield 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30484680/
我正在尝试组合 2 个 jcomboboxes。 1 组合框用于显示费用类别。第二个组合框正在从文本文件中读取文件以显示产品类型。如果我更改第一个组合框,我希望第二个组合框会根据用户在第一个组合框中选
我希望一个 JComboBox 的值根据另一个 JComboBox 的值进行更改。在一个 JComboBox 中,我显示了从数据库检索的医生名称。根据该名称,我想在另一个 JComboBox 中显示医
我有课 public class Speciality { String specName; String eduProgram; } 和 TreeMap Map> treeM
我有一个 JComboBox。我希望它能够工作,以便如果选择某个项目(“其他”),立即在同一个组合框中显示更多项目(类似于子菜单,但在组合框内)。我很难让这个工作发挥作用。 有人对如何做到这一点有任何
如何从 JComboBox 中进行选择,删除或添加选择到另一个 JComboBox? 更具体地说,我有一个 JComboBox,其中包含一年中的月份作为选择。我还有另一个,里面有这个月的日子。虽然 3
我有一个用例(在 Java Swing 中),其中某个 JTable 列必须可以通过 JComboBox 编辑器进行编辑,但可选择的值列表取决于该行的域对象(我使用自定义 TableModel)。 C
我们可以在执行 jFrame 后根据点击另一个 jcomboBox 的项目来启用 jcomboBox 吗?这可能吗? 最佳答案 JComboBox comboBox_1 = new JComboBox
我有 7 个 jcombobox,它们都以相同的默认选择开头。如果在前面的 jcombobox 中选择了默认值以外的内容,如何启用下一个? if ( ! (custData1.equa
我有 2 个 JComboBox,在第一个 JComboBox 上选择一个项目后,第二个会从数据库中填充。问题是,每次我输入一个字母时,第二个 jcombobox 都会填充。我想让第二个 jcombo
您好,我正在尝试将一个对象从一个组合框传输到另一个组合框,反之亦然。为了实现这一目标,我使用 actionListeners 或 ItemListeners,不幸的是它们没有回答我的问题,或者可能只是
当我从 JComboBox A 中选择项目时,我尝试在 JComboBox B 中显示值。到目前为止,当我从 JComboBox A 中选择一个值时,没有任何反应。这是我的数据和代码。例如,如果我从
我写了这个简单的程序: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tab
我正在制作一个医疗药物软件程序,我希望用户能够从由类别数组填充的 jcombo 框中选择疾病类别(呼吸系统疾病、心血管疾病等)。 选择此类别后,我希望另一个 jcombobox 仅填充与该类别相关的药
这段代码很草率,我也欢迎一些反馈。 我正在尝试根据另一个 JComboBox 的值更改 JComboBox 的值。还有一个额外的复杂性,因为我使用一个额外的类来确定要返回的字符串数组(请参阅我之前的问
我已经加载了对象的 JComboBox,这些对象有自己的“toString”方法。该组合框用于选择并通过该组合框上的“ActionListener”返回该对象。一切工作正常,直到我决定添加通过在此组合
所以我的问题是 JComboBoxs 和 ActionListeners。我将制作一个新的简化代码来尝试代表我的原始代码中的问题。我想要一个 JComboBox 添加一个 JComboBox,然后 J
我的转换器程序中有 3 个 JCombobox,其中一个用于类别,两个用于选择要执行的功能;我希望程序做的是,当用户在一个 JCombobox 中选择一个类别时,两个 JCombobox 将自动更改与
所以,这就是问题所在:我需要 3 个或更多 jComboBox(都具有相同的项目)相互连接,这样当我在任何 jComboBox 中选择一个项目时,该项目将在所有其他 jComboBox 中消失。具体例
我用这样的东西制作了一个图形用户界面: String[] days29 = {"1",....."29"}; String[] days30 = {"1",....."30"}; String[] d
我有一个小应用程序,它通过 JPA 从 MySQL 数据库生成统计图表。为了选择要包含在统计信息中的数据库组件,我安装了 2 个 JComboBoxes。第一个 JComboBox 填充了 Categ
我是一名优秀的程序员,十分优秀!