Java JDialog返回值?
如果这个问题已经被问了很多次,很抱歉,但是我有一个自定义JDialog。它具有自定义背景,并显示在调用它的帧上,接收消息,然后在选择调用它的帧的选项时返回是或否(0或1)Java JDialog返回值?,java,swing,jframe,return,jdialog,Java,Swing,Jframe,Return,Jdialog,如果这个问题已经被问了很多次,很抱歉,但是我有一个自定义JDialog。它具有自定义背景,并显示在调用它的帧上,接收消息,然后在选择调用它的帧的选项时返回是或否(0或1) public class JYOptionPane { @SuppressWarnings("deprecation") public static void showJY_YES_NO_Dialog(final JFrame frame, String Question) { darken(frame);
public class JYOptionPane {
@SuppressWarnings("deprecation")
public static void showJY_YES_NO_Dialog(final JFrame frame, String Question) {
darken(frame);
frame.disable();
final JDialog OptionPane = new JDialog();
OptionPane.setAlwaysOnTop(true);
OptionPane.setBounds(
(int) (frame.getX() + (frame.getSize().getWidth() / 2))
- (350 / 2), (int) (frame.getY() + (frame.getSize()
.getHeight() / 2)) - (200 / 2), 350, 200);
OptionPane.setResizable(false);
OptionPane.setUndecorated(true);
ImageIcon icon = new ImageIcon(
LoginFrame.class.getResource("/Images/bgprompt.png"));
Image backImage = icon.getImage();
ImagePanel contentPane = new ImagePanel();
contentPane.setBackgroundImage(backImage);
OptionPane.setContentPane(contentPane);
OptionPane.setBackground(new Color(0, 0, 0, 0));
JLabel lblQuestion = new JLabel(Question);
lblQuestion.setBounds(0, 40, 350, 30);
lblQuestion.setFont(new Font("Calibria", Font.PLAIN, 20));
lblQuestion.setForeground(Color.white);
lblQuestion.setHorizontalAlignment(SwingConstants.CENTER);
lblQuestion.setVerticalAlignment(SwingConstants.CENTER);
lblQuestion.setAlignmentY(JComponent.CENTER_ALIGNMENT);
OptionPane.add(lblQuestion);
JYButton btnYes = new JYButton(
new ImageIcon(LoginFrame.class.getResource("/buttons/default.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaultprs.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaulthv.png")));
btnYes.setText("Yes");
btnYes.setFont(new Font("Arial", Font.BOLD, 17));
btnYes.setForeground(Color.white);
btnYes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
LoginFrame.setOPintNo(0);
Class<? extends JFrame> f = frame.getClass();
try {
f.newInstance().setVisible(true);
frame.dispose();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
OptionPane.dispose();
}
});
btnYes.setBounds(40, 200 / 2 - 31 / 2 + 50, 121, 31);
OptionPane.add(btnYes);
OptionPane.getRootPane().setDefaultButton(btnYes);
JYButton btnNo = new JYButton(new ImageIcon(
LoginFrame.class.getResource("/buttons/default.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaultprs.png")),
new ImageIcon(LoginFrame.class.getResource("/buttons/defaulthv.png")));
btnNo.setText("No");
btnNo.setFont(new Font("Arial", Font.BOLD, 17));
btnNo.setForeground(Color.white);
btnNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
LoginFrame.setOPintNo(1);
Class<? extends JFrame> f = frame.getClass();
try {
f.newInstance().setVisible(true);
frame.dispose();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
OptionPane.dispose();
}
});
btnNo.setBounds(189, 200 / 2 - 31 / 2 + 50, 121, 31);
OptionPane.add(btnNo);
OptionPane.setVisible(true);
}
}
公共类JYOptionPane{
@抑制警告(“弃用”)
公共静态void showJY_YES_NO_对话框(最终JFrame,字符串问题){
变暗(帧);
frame.disable();
最终JDialog选项窗格=新JDialog();
OptionPane.setAlwaysOnTop(真);
OptionPane.setBounds(
(int)(frame.getX()+(frame.getSize().getWidth()/2))
-(350/2),(int)(frame.getY()+(frame.getSize())
.getHeight()/2)-(200/2),350200);
OptionPane.SetResizeable(假);
OptionPane.SetUndecorrated(真);
ImageIcon图标=新的ImageIcon(
LoginFrame.class.getResource(“/Images/bgprompt.png”);
Image backImage=icon.getImage();
ImagePanel contentPane=新建ImagePanel();
contentPane.setBackgroundImage(backImage);
OptionPane.setContentPane(contentPane);
OptionPane.setBackground(新颜色(0,0,0,0));
JLabel lblQuestion=新的JLabel(问题);
LBL问题。挫折(0,40,350,30);
lblQuestion.setFont(新字体(“Calibria”,Font.PLAIN,20));
lblQuestion.setForeground(颜色.白色);
lblQuestion.setHorizontalAlignment(SwingConstants.CENTER);
lblQuestion.setverticalignment(SwingConstants.CENTER);
lblQuestion.setAlignmentY(JComponent.CENTER_ALIGNMENT);
选项窗格。添加(LBL问题);
JYButton btnYes=新的JYButton(
新的图像图标(LoginFrame.class.getResource(“/buttons/default.png”),
新的图像图标(LoginFrame.class.getResource(“/buttons/defaultprs.png”),
新的图像图标(LoginFrame.class.getResource(“/buttons/defaulthv.png”);
btnYes.setText(“是”);
setFont(新字体(“Arial”,Font.BOLD,17));
设置前景(颜色为白色);
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件arg0){
LoginFrame.setOPintNo(0);
类您可能需要查看文档。首先,您不需要向您的类传递文档,只需将其删除即可。请查看以下示例:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
private void createAndShowGUI() {
JButton button = new JButton("Show dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MyOptionPane optionPane = new MyOptionPane();
int option = optionPane.showYesNoMessage("Close frame", "Do you really want to close the frame?");
if(option == MyOptionPane.YES) {
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
}
});
JPanel content = new JPanel();
content.add(new JLabel("Test:"));
content.add(button);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class MyOptionPane {
public static final int YES = 0;
public static final int NO = -1;
private int choice = NO;
public int showYesNoMessage(String title, String message) {
JLabel label = new JLabel(message);
JButton yesButton = new JButton("Yes");
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = YES;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JButton noButton = new JButton("No");
noButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = NO;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JPanel buttons = new JPanel();
buttons.add(yesButton);
buttons.add(noButton);
JPanel content = new JPanel(new BorderLayout(8, 8));
content.add(label, BorderLayout.CENTER);
content.add(buttons, BorderLayout.SOUTH);
JDialog dialog = new JDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);
dialog.setTitle(title);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return choice;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}
离题
关于和的使用,@AndrewThompson总是说:
Java GUI可能必须在不同的平台上工作
屏幕分辨率&使用不同的plaf。因此它们不是
有助于准确放置组件。组织组件
对于健壮的GUI,使用布局管理器或
它们,以及空白区域的布局填充和边框
因此,在使用Swing组件时,您应该完全避免使用setBounds()
。您可能需要查看文档。首先,您不需要向类传递一个,您可以简单地将其删除。请看以下示例:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
private void createAndShowGUI() {
JButton button = new JButton("Show dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MyOptionPane optionPane = new MyOptionPane();
int option = optionPane.showYesNoMessage("Close frame", "Do you really want to close the frame?");
if(option == MyOptionPane.YES) {
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
}
});
JPanel content = new JPanel();
content.add(new JLabel("Test:"));
content.add(button);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class MyOptionPane {
public static final int YES = 0;
public static final int NO = -1;
private int choice = NO;
public int showYesNoMessage(String title, String message) {
JLabel label = new JLabel(message);
JButton yesButton = new JButton("Yes");
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = YES;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JButton noButton = new JButton("No");
noButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = NO;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JPanel buttons = new JPanel();
buttons.add(yesButton);
buttons.add(noButton);
JPanel content = new JPanel(new BorderLayout(8, 8));
content.add(label, BorderLayout.CENTER);
content.add(buttons, BorderLayout.SOUTH);
JDialog dialog = new JDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);
dialog.setTitle(title);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return choice;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}
离题
关于和的使用,@AndrewThompson总是说:
Java GUI可能必须在不同的平台上工作
屏幕分辨率&使用不同的plaf。因此它们不是
有助于准确放置组件。组织组件
对于健壮的GUI,使用布局管理器或
它们,以及空白区域的布局填充和边框
因此,在使用Swing组件时,您应该完全避免使用setBounds()
。您可能需要查看文档。首先,您不需要向类传递一个,您可以简单地将其删除。请看以下示例:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
private void createAndShowGUI() {
JButton button = new JButton("Show dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MyOptionPane optionPane = new MyOptionPane();
int option = optionPane.showYesNoMessage("Close frame", "Do you really want to close the frame?");
if(option == MyOptionPane.YES) {
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
}
});
JPanel content = new JPanel();
content.add(new JLabel("Test:"));
content.add(button);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class MyOptionPane {
public static final int YES = 0;
public static final int NO = -1;
private int choice = NO;
public int showYesNoMessage(String title, String message) {
JLabel label = new JLabel(message);
JButton yesButton = new JButton("Yes");
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = YES;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JButton noButton = new JButton("No");
noButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = NO;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JPanel buttons = new JPanel();
buttons.add(yesButton);
buttons.add(noButton);
JPanel content = new JPanel(new BorderLayout(8, 8));
content.add(label, BorderLayout.CENTER);
content.add(buttons, BorderLayout.SOUTH);
JDialog dialog = new JDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);
dialog.setTitle(title);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return choice;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}
离题
关于和的使用,@AndrewThompson总是说:
Java GUI可能必须在不同的平台上工作
屏幕分辨率&使用不同的plaf。因此它们不是
有助于准确放置组件。组织组件
对于健壮的GUI,使用布局管理器或
它们,以及空白区域的布局填充和边框
因此,在使用Swing组件时,您应该完全避免使用setBounds()
。您可能需要查看文档。首先,您不需要向类传递一个,您可以简单地将其删除。请看以下示例:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
private void createAndShowGUI() {
JButton button = new JButton("Show dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MyOptionPane optionPane = new MyOptionPane();
int option = optionPane.showYesNoMessage("Close frame", "Do you really want to close the frame?");
if(option == MyOptionPane.YES) {
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
}
});
JPanel content = new JPanel();
content.add(new JLabel("Test:"));
content.add(button);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class MyOptionPane {
public static final int YES = 0;
public static final int NO = -1;
private int choice = NO;
public int showYesNoMessage(String title, String message) {
JLabel label = new JLabel(message);
JButton yesButton = new JButton("Yes");
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = YES;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JButton noButton = new JButton("No");
noButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
choice = NO;
JButton button = (JButton)e.getSource();
SwingUtilities.getWindowAncestor(button).dispose();
}
});
JPanel buttons = new JPanel();
buttons.add(yesButton);
buttons.add(noButton);
JPanel content = new JPanel(new BorderLayout(8, 8));
content.add(label, BorderLayout.CENTER);
content.add(buttons, BorderLayout.SOUTH);
JDialog dialog = new JDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);
dialog.setTitle(title);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return choice;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}
离题
关于和的使用,@AndrewThompson总是说:
Java GUI可能必须在不同的平台上工作
屏幕分辨率&使用不同的plaf。因此它们不是
有助于准确放置组件。组织组件
对于健壮的GUI,使用布局管理器或
它们,以及空白区域的布局填充和边框
因此,在使用Swing组件时,应该完全避免使用setBounds()