Java Jinternalframe自身的按钮不起作用
我已经创建了JInternalFrame,并在其中添加了几个按钮;然后它就不能正常工作。例如,如果一个帧被最大化,如果试图最小化另一个面板,那么最大面板也会被最小化。然而,如果使所有都为真,那么它就能正常工作。我的目标是创建我自己的按钮,它将执行最小/最大/还原和关闭。请查找下面的代码Java Jinternalframe自身的按钮不起作用,java,swing,layout,jinternalframe,Java,Swing,Layout,Jinternalframe,我已经创建了JInternalFrame,并在其中添加了几个按钮;然后它就不能正常工作。例如,如果一个帧被最大化,如果试图最小化另一个面板,那么最大面板也会被最小化。然而,如果使所有都为真,那么它就能正常工作。我的目标是创建我自己的按钮,它将执行最小/最大/还原和关闭。请查找下面的代码 package Project; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import
package Project;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
public class Test5 implements MouseListener {
private JDesktopPane pane;
public int mouseCount;
public static void main(String[] args) {
new Test5();
}
private int xpos = 0;
private int ypos = 0;
public Test5() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exp) {
exp.printStackTrace();
}
pane = new Test5.DesktopPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
pane.add(newInternalFrame());
pane.add(newInternalFrame());
pane.add(newInternalFrame());
JFrame frame = new JFrame();
frame.add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public JInternalFrame newInternalFrame() {
final JInternalFrame inf = new JInternalFrame("Blah", false, false, false, true);
inf.setLocation(xpos, ypos);
inf.setSize(200, 100);
inf.setVisible(true);
xpos += 50;
ypos += 50;
JPanel jp = new JPanel();
JLabel jl = new JLabel("panel" + xpos);
JButton jb = new JButton("_");
JButton jb2 = new JButton("[]");
JButton jb3 = new JButton("X");
inf.setLayout(new GridLayout(2, 2));
jp.add(jl);
jp.add(jb);
jp.add(jb2);
jp.add(jb3);
inf.add(jp);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
if (inf.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
pane.remove(inf);
pane.add(inf, JDesktopPane.DEFAULT_LAYER);
pane.revalidate();
pane.repaint();
}
inf.pack();
inf.setIcon(true);
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
if (inf.isMaximum()) {//restore
inf.pack();
} else {//maximize
inf.setMaximum(true);
}
pane.remove(inf);
pane.add(inf, JDesktopPane.FRAME_CONTENT_LAYER);
pane.revalidate();
pane.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
inf.dispose();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) inf.getUI()).getNorthPane();
inf.remove(titlePane);
return inf;
}
public class DesktopPane extends JDesktopPane {
@Override
public void doLayout() {
super.doLayout();
List<Component> icons = new ArrayList<Component>(25);
int maxLayer = 0;
for (Component comp : getComponents()) {
if (comp instanceof JInternalFrame.JDesktopIcon) {
icons.add(comp);
maxLayer = Math.max(getLayer(comp), maxLayer);
}
}
maxLayer++;
int x = 0;
for (Component icon : icons) {
int y = getHeight() - icon.getHeight();
icon.setLocation(x, y);
x += icon.getWidth();
setLayer(icon, maxLayer);
}
}
}
@Override
public void mouseClicked(MouseEvent me) {
mouseCount=me.getClickCount();
if(mouseCount==2)
{
System.out.println("clicked"+mouseCount);
}
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
System.out.println("clicked"+mouseCount);
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
成套工程;
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.GridLayout;
导入java.awt.event.*;
导入java.beans.PropertyVetoException;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JButton;
导入javax.swing.JDesktopPane;
导入javax.swing.JFrame;
导入javax.swing.JInternalFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.plaf.basic.BasicInternalFrameTitlePane;
导入javax.swing.plaf.basic.BasicInternalFrameUI;
公共类Test5实现MouseListener{
专用JDesktopPane窗格;
公共国际鼠标计数;
公共静态void main(字符串[]args){
新Test5();
}
私有int xpos=0;
私有int ypos=0;
公共测试5(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(异常扩展){
exp.printStackTrace();
}
pane=newtest5.DesktopPane(){
@凌驾
公共维度getPreferredSize(){
返回新维度(400400);
}
};
添加(newInternalFrame());
添加(newInternalFrame());
添加(newInternalFrame());
JFrame=新JFrame();
框架。添加(窗格);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共JInternalFrame newInternalFrame(){
最终JInternalFrame inf=新JInternalFrame(“废话”,假,假,假,真);
inf.setLocation(xpos、YPO);
inf.setSize(200100);
inf.setVisible(真);
xpos+=50;
ypos+=50;
JPanel jp=新的JPanel();
JLabel jl=新的JLabel(“面板”+XPO);
JButton jb=新JButton(“”);
JButton jb2=新JButton(“[]”);
JButton jb3=新JButton(“X”);
inf.setLayout(新的GridLayout(2,2));
jp.add(jl);;
jp.add(jb),;
jp.add(jb2);
jp.add(jb3);
inf.add(jp);
jb.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
试一试{
if(inf.getLayer()==JDesktopPane.FRAME\u CONTENT\u LAYER){
窗格。删除(inf);
添加(inf,JDesktopPane.DEFAULT_层);
窗格。重新验证();
pane.repaint();
}
inf.pack();
inf.setIcon(真);
}捕获(PropertyVetoException-ex){
例如printStackTrace();
}
}
});
jb2.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
试一试{
如果(inf.ismax()){//restore
inf.pack();
}否则{//
inf.setMaximum(真);
}
窗格。删除(inf);
添加(inf,JDesktopPane.FRAME\u CONTENT\u LAYER);
窗格。重新验证();
pane.repaint();
}捕获(例外e){
e、 printStackTrace();
}
}
});
jb3.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
试一试{
inf.dispose();
}捕获(例外情况除外){
例如printStackTrace();
}
}
});
基本内部框架标题页=(基本内部框架标题页)((基本内部框架)inf.getUI()).getNorthPane();
inf.remove(titlePane);
返回inf;
}
公共类DesktopPane扩展了JDesktopPane{
@凌驾
公共空间布局(){
super.doLayout();
列表图标=新的ArrayList(25);
int maxLayer=0;
对于(组件comp:getComponents()){
if(JInternalFrame.JDesktopIcon的组件实例){
图标。添加(comp);
maxLayer=Math.max(getLayer(comp),maxLayer);
}
}
maxLayer++;
int x=0;
用于(组件图标:图标){
int y=getHeight()-icon.getHeight();
图标。设置位置(x,y);
x+=icon.getWidth();
setLayer(图标,maxLayer);
}
}
}
@凌驾
公共无效mouseClicked(MouseEvent me){
mouseCount=me.getClickCount();
如果(鼠标计数==2)
{
System.out.println(“单击”+鼠标计数);
}
//TODO自动生成的方法存根
}
@凌驾
公共无效鼠标事件(鼠标事件arg0){
System.out.println(“单击”+鼠标计数);
//TODO自动生成的方法存根
}
@凌驾
public void mouseexitted(MouseEvent arg0){
//TODO自动生成的方法存根
}
@奥夫
public class Test5 implements MouseListener {
private JDesktopPane pane;
public int mouseCount;
public static void main(String[] args) {
new Test5();
}
private int xpos = 0;
private int ypos = 0;
public Test5() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exp) {
exp.printStackTrace();
}
pane = new Test5.DesktopPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
pane.add(newInternalFrame(), 10);
pane.add(newInternalFrame(), 10);
pane.add(newInternalFrame(), 10);
JFrame frame = new JFrame();
frame.add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public JInternalFrame newInternalFrame() {
final JInternalFrame inf = new JInternalFrame("Blah", false, false, false, true);
inf.setLocation(xpos, ypos);
inf.setSize(300, 300);
inf.setVisible(true);
xpos += 50;
ypos += 50;
JPanel jp = new JPanel();
JLabel jl = new JLabel("panel" + xpos);
JButton jb = new JButton("_");
JButton jb2 = new JButton("[]");
JButton jb3 = new JButton("X");
inf.setLayout(new GridLayout(2, 2));
jp.add(jl);
jp.add(jb);
jp.add(jb2);
jp.add(jb3);
inf.add(jp);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
inf.setIcon(true);
// if (inf.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
// pane.remove(inf);
// pane.add(inf, JDesktopPane.DEFAULT_LAYER);
// pane.revalidate();
// pane.repaint();
// }
// inf.pack();
// inf.setIcon(true);
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
inf.setMaximum(!inf.isMaximum());
if (inf.isMaximum()) {
((JLayeredPane)inf.getParent()).setLayer(inf, 0);
} else {
((JLayeredPane)inf.getParent()).setLayer(inf, 10);
}
// if (inf.isMaximum()) {//restore
// inf.pack();
// } else {//maximize
// inf.setMaximum(true);
//
// }
// pane.remove(inf);
// pane.add(inf, JDesktopPane.FRAME_CONTENT_LAYER);
// pane.revalidate();
// pane.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Container parent = inf.getParent();
inf.dispose();
parent.remove(inf);
// try {
// inf.dispose();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
}
});
// BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) inf.getUI()).getNorthPane();
// inf.remove(titlePane);
return inf;
}
public class DesktopPane extends JDesktopPane {
@Override
public void doLayout() {
super.doLayout();
List<Component> icons = new ArrayList<Component>(25);
int maxLayer = 0;
for (Component comp : getComponents()) {
if (comp instanceof JInternalFrame.JDesktopIcon) {
icons.add(comp);
maxLayer = Math.max(getLayer(comp), maxLayer);
}
}
maxLayer++;
int x = 0;
for (Component icon : icons) {
int y = getHeight() - icon.getHeight();
icon.setLocation(x, y);
x += icon.getWidth();
setLayer(icon, maxLayer);
}
}
}
@Override
public void mouseClicked(MouseEvent me) {
mouseCount = me.getClickCount();
if (mouseCount == 2) {
System.out.println("clicked" + mouseCount);
}
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
System.out.println("clicked" + mouseCount);
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) inf.getUI()).getNorthPane();
inf.remove(titlePane);
pane.setDesktopManager(new DefaultDesktopManager());