Java阻止最后一个JFrame被聚焦成一个JOptionPane [英] Java Block last JFrame from being focused like a JOptionPane

查看:215
本文介绍了Java阻止最后一个JFrame被聚焦成一个JOptionPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序。当我点击一个按钮时,它会打开第二个JFrame。 (注意:我不想使用JOptionPane,因为它太基本了)。我想,就像JOptionPane,我的第一个框架不能被集中,如果点击,第二个将闪烁像JOP。



基本上: - 我想将第二个Jframe作为两个焦点框架之一,第一个Jframe在第二个关闭之前不能点击。 解决方案

您想要:


  • 使用 模式 第二个JFrame

  • 或者使用 JOptionPane ,它实际上是一个带有一点额外窗口修饰的模态JDialog。这些人比你意识到的更强大,他们可以容纳任何复杂的图形用户界面(GUI),包括一个充满大量组件的图形用户界面。






例如:

  import java.awt。*; 
import java.awt.event。*;
import java.util.HashMap;
import java.util.Map;

import javax.swing。*;

@SuppressWarnings(serial)
public class ComplexOptionPane extends JPanel {
private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel();
private JTextArea textArea = new JTextArea(12,30);

public ComplexOptionPane(){
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setFont(new Font(Font.MONOSPACED,Font.PLAIN,16));
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JButton(new GetAction(Get Player Information){
$ b $ @Override
public void actionPerformed(ActionEvent arg0){
int result = JOptionPane.showConfirmDialog(null,playerEditorPanel,
Edit Player JOptionPane,JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.OK_OPTION){
for (PlayerEditorPanel.FieldTitle fieldTitle:PlayerEditorPanel.FieldTitle $ b $ .values()){
textArea.append(String.format(%10s:%s%n,
fieldTitle.getTitle() ,
playerEditorPanel.getFieldText(fieldTitle)));
}
}
}
}));
setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
setLayout(new BorderLayout(5,5));
add(new JScrollPane(textArea),BorderLayout.CENTER);
add(bottomPanel,BorderLayout.PAGE_END);


private static void createAndShowGui(){
ComplexOptionPane mainPanel = new ComplexOptionPane();

JFrame frame = new JFrame(ComplexOptionPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane()。add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);


public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGui();
}
});


$ b @SuppressWarnings(serial)$ b $ class PlayerEditorPanel扩展了JPanel {
enum FieldTitle {
NAME(Name ),速度(速度),力量(力量),健康(健康);
私人字符串标题;

私人FieldTitle(字符串标题){
this.title = title;
}

public String getTitle(){
return title;
}
};

private static final Insets WEST_INSETS = new Insets(5,0,5,5);
private static final Insets EAST_INSETS = new Insets(5,5,5,0);
私人地图< FieldTitle,JTextField> fieldMap = new HashMap< FieldTitle,JTextField>();
$ b $ public PlayerEditorPanel(){
setLayout(new GridBagLayout());
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(Player Editor),
BorderFactory.createEmptyBorder(5,5,5)));
GridBagConstraints gbc;
for(int i = 0; i< FieldTitle.values().length; i ++){
FieldTitle fieldTitle = FieldTitle.values()[i];
gbc = createGbc(0,i);
add(new JLabel(fieldTitle.getTitle()+:,JLabel.LEFT),gbc);
gbc = createGbc(1,i);
JTextField textField = new JTextField(10);
add(textField,gbc);

fieldMap.put(fieldTitle,textField);


$ b $ private GridBagConstraints createGbc(int x,int y){
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;

gbc.anchor =(x == 0)? GridBagConstraints.WEST:GridBagConstraints.EAST;
gbc.fill =(x == 0)? GridBagConstraints.BOTH
:GridBagConstraints.HORIZONTAL;

gbc.insets =(x == 0)? WEST_INSETS:EAST_INSETS;
gbc.weightx =(x == 0)? 0.1:1.0;
gbc.weighty = 1.0;
返回gbc;


public String getFieldText(FieldTitle fieldTitle){
return fieldMap.get(fieldTitle).getText();
}

}

代码借用我的回答

I got a program. When I clic on a button, it opens a 2nd JFrame. (NB: I do not not want to use a JOptionPane because it is too basic). I would like, just like the JOptionPane, that my first Frame cannot be focusable and if clicked, the 2nd one will blink like the JOP.

Basicly: - I want to have the 2nd Jframe to be the focused frame out of the two and the 1st one cannot be clicked until the 2nd is closed.

解决方案

You want to:

  • Either use a modal JDialog in place of the second JFrame
  • Or use a JOptionPane, which really is a modal JDialog with a little extra window dressing. These guys are more powerful than you realize, and they can hold most any complex GUI, including one filled with tons of components, you give to it.

For example:

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

@SuppressWarnings("serial")
public class ComplexOptionPane extends JPanel {
   private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel();
   private JTextArea textArea = new JTextArea(12, 30);

   public ComplexOptionPane() {
      textArea.setEditable(false);
      textArea.setFocusable(false);
      textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
      JPanel bottomPanel = new JPanel();
      bottomPanel.add(new JButton(new AbstractAction("Get Player Information") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            int result = JOptionPane.showConfirmDialog(null, playerEditorPanel,
                  "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION,
                  JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
               for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle
                     .values()) {
                  textArea.append(String.format("%10s: %s%n",
                        fieldTitle.getTitle(),
                        playerEditorPanel.getFieldText(fieldTitle)));
               }
            }
         }
      }));
      setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      setLayout(new BorderLayout(5, 5));
      add(new JScrollPane(textArea), BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   private static void createAndShowGui() {
      ComplexOptionPane mainPanel = new ComplexOptionPane();

      JFrame frame = new JFrame("ComplexOptionPane");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
   enum FieldTitle {
      NAME("Name"), SPEED("Speed"), STRENGTH("Strength"), HEALTH("Health");
      private String title;

      private FieldTitle(String title) {
         this.title = title;
      }

      public String getTitle() {
         return title;
      }
   };

   private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
   private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
   private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();

   public PlayerEditorPanel() {
      setLayout(new GridBagLayout());
      setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Player Editor"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));
      GridBagConstraints gbc;
      for (int i = 0; i < FieldTitle.values().length; i++) {
         FieldTitle fieldTitle = FieldTitle.values()[i];
         gbc = createGbc(0, i);
         add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc);
         gbc = createGbc(1, i);
         JTextField textField = new JTextField(10);
         add(textField, gbc);

         fieldMap.put(fieldTitle, textField);
      }
   }

   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;

      gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
      gbc.fill = (x == 0) ? GridBagConstraints.BOTH
            : GridBagConstraints.HORIZONTAL;

      gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
      gbc.weightx = (x == 0) ? 0.1 : 1.0;
      gbc.weighty = 1.0;
      return gbc;
   }

   public String getFieldText(FieldTitle fieldTitle) {
      return fieldMap.get(fieldTitle).getText();
   }

}

code borrowed from this answer of mine.

这篇关于Java阻止最后一个JFrame被聚焦成一个JOptionPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆