我要問同樣的問題(它有錯誤的答案),從3年前...
不,答案是完全正確。
如何從一個類中獲取ComboBox的選定項並在新類中使用該選定項的值。
同樣,Reimeus告訴你如何正確地做到這一點。
比方說,源類和其他類。我想從其他類的源類打印項目3(在ComboBox中的第三項)。
不知道你的"item 3"
的意思,但如果它是另一個選擇的項目,你又指的答案是正確的。
我已經使用了上面的答案。但是,它只返回第一個項目。因爲我認爲每次我從源類調用構造函數時,都會將所選項重新啓動到第一項。
而這正是你的問題 - 你不應該重新調用構造函數,因爲這會給你錯誤的參考。它會給你一個全新的GUI參考,一個是圖形用戶界面,沒有顯示。您需要參考當前查看的感興趣的類。你如何做到這一點將取決於你的程序結構 - 請展示更多。
例如:注意下面的代碼有兩個Jbutton將,一個沒有它的ActionListener中正確的事(實際上,一個AbstractAction,但類似結構),以及一個做事不正確:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class GetCombo extends JFrame {
// the displayed ClassWithCombo object
private ClassWithCombo classWithCombo = new ClassWithCombo(this);;
private int columns = 10;
private JTextField textField1 = new JTextField(columns);
private JTextField textField2 = new JTextField(columns);
public GetCombo() {
classWithCombo.pack();
classWithCombo.setLocationByPlatform(true);
classWithCombo.setVisible(true);
setLayout(new FlowLayout());
add(textField1);
add(new JButton(new AbstractAction("Doing It Right") {
@Override
public void actionPerformed(ActionEvent e) {
// use the ClassWithCombo reference that is already displayed
String selectedString = classWithCombo.getSelectedItem();
textField1.setText(selectedString);
}
}));
add(textField2);
add(new JButton(new AbstractAction("Doing It Wrong") {
@Override
public void actionPerformed(ActionEvent e) {
// create a new non-displayed ClassWithCombo reference.
ClassWithCombo classWithCombo = new ClassWithCombo(GetCombo.this);
String selectedString = classWithCombo.getSelectedItem();
textField2.setText(selectedString);
}
}));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GetCombo getCombo = new GetCombo();
getCombo.setTitle("Get Combo Example");
getCombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getCombo.pack();
getCombo.setLocationRelativeTo(null);
getCombo.setVisible(true);
});
}
}
@SuppressWarnings("serial")
class ClassWithCombo extends JDialog {
private static final String[] DATA = { "Mon", "Tues", "Wed", "Thurs", "Fri" };
private JComboBox<String> combo = new JComboBox<>(DATA);
public ClassWithCombo(JFrame frame) {
super(frame, "Holds Combo Dialog", ModalityType.MODELESS);
setLayout(new FlowLayout());
setPreferredSize(new Dimension(300, 250));
add(combo);
}
public String getSelectedItem() {
return (String) combo.getSelectedItem();
}
}
編輯
閱讀的最新文章後,我現在看到你正在試圖打開包含窗口的窗口組合呈現給用戶得到主程序在運行時使用的信息。在這種情況下,最好的辦法是使用對話框,即在對話框打開後凍結主窗口中的程序流的對話框,該對話框不允許用戶在對話框中與主窗口進行交互已經打開,然後在對話框窗口關閉時恢復程序流程和用戶交互。
請看下面對我的示例程序的更改。在這裏,我改變了用於JDialog的超級構造函數,使其成爲APPLICATION_MODAL,具有上述行爲。然後在主窗口的Butotn的ActionListener中打開對話框。由於組合窗口是一個模態對話框 - 程序不會從組合窗口中提取信息,直到它被關閉 - 這個位是非常重要的。這樣,您的主程序就可以獲取用戶的選擇,而不總是組合框中的第一項。我添加了一個名爲submitButton的新JButton,它所做的就是關閉當前窗口。如果需要,您可以將ActionListener添加到JComboBox中,以便在選擇完成後關閉它的窗口,但這不允許用戶改變主意,所以我更喜歡使用提交按鈕。
請注意更改標記爲\\ !!
評論。
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class GetCombo2 extends JFrame {
// the displayed ClassWithCombo object
private ClassWithCombo classWithCombo = new ClassWithCombo(this);;
private int columns = 10;
private JTextField textField1 = new JTextField(columns);
public GetCombo2() {
classWithCombo.pack();
classWithCombo.setLocationByPlatform(true);
// !! don't do this here
// classWithCombo.setVisible(true);
setLayout(new FlowLayout());
textField1.setFocusable(false);
add(textField1);
add(new JButton(new AbstractAction("Open Combo as a Dialog") {
@Override
public void actionPerformed(ActionEvent e) {
// open combo dialog as a **modal** dialog:
classWithCombo.setVisible(true);
// this won't run until the dialog has been closed
String selectedString = classWithCombo.getSelectedItem();
textField1.setText(selectedString);
}
}));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GetCombo2 getCombo = new GetCombo2();
getCombo.setTitle("Get Combo Example");
getCombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getCombo.pack();
getCombo.setLocationRelativeTo(null);
getCombo.setVisible(true);
});
}
}
@SuppressWarnings("serial")
class ClassWithCombo extends JDialog {
private static final String[] DATA = { "Mon", "Tues", "Wed", "Thurs", "Fri" };
private JComboBox<String> combo = new JComboBox<>(DATA);
public ClassWithCombo(JFrame frame) {
// !! don't make it MODELESS
// !! super(frame, "Holds Combo Dialog", ModalityType.MODELESS);
// !! note the change. Made it APPLICATION_MODAL
super(frame, "Holds Combo Dialog", ModalityType.APPLICATION_MODAL);
JButton submitButton = new JButton(new AbstractAction("Submit") {
// !! add an ActionListener to close window when the submit button
// has been pressed.
@Override
public void actionPerformed(ActionEvent e) {
ClassWithCombo.this.setVisible(false);
}
});
setLayout(new FlowLayout());
setPreferredSize(new Dimension(300, 250));
add(combo);
add(submitButton);
}
public String getSelectedItem() {
return (String) combo.getSelectedItem();
}
}
HTML?搖擺? AWT? JavaFX的? SWT? – MadProgrammer
這是javax.swing.JFrame @MadProgrammer –