Java 如何仅从JFrame中删除最大化按钮?
我有一个问题,想从中删除最大化按钮 我写了下面的代码,但它从我的Java 如何仅从JFrame中删除最大化按钮?,java,swing,jframe,titlebar,maximize-window,Java,Swing,Jframe,Titlebar,Maximize Window,我有一个问题,想从中删除最大化按钮 我写了下面的代码,但它从我的JFrame中删除了maximize、minimize和close JFrame frame = new JFrame(); frame.add(kart); frame.setUndecorated(true); frame.setVisible(true); frame.setSize(400, 400); 我只想从JFrame中删除最大化按钮。您不能从JFrame中删除该按钮。改用JDialog。它没有最大化按钮。描述了如何
JFrame
中删除了maximize、minimize和close
JFrame frame = new JFrame();
frame.add(kart);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setSize(400, 400);
我只想从
JFrame
中删除最大化按钮。您不能从JFrame
中删除该按钮。改用JDialog
。它没有最大化按钮。描述了如何在没有最大化和最小化按钮的情况下实现“JFrame”。
JFrame frame = new JFrame();
frame.add(kart);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setSize(400, 400);
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog; import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JDialog {
public Test(JFrame frame, String str) {
super(frame, str);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args) {
try {
Test myFrame = new Test(new JFrame(), "Removing maximize button");
JPanel panel = new JPanel();
panel.setSize(100, 100);
myFrame.add(panel);
myFrame.setSize(100, 100);
myFrame.setVisible(true);
} catch (IllegalArgumentException e) {
System.exit(0);
}
} }
您只需在JDialog中“装入”JFrame即可:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RemoveMaxAndMinButton extends JDialog{
public RemoveMaxAndMinButton(JFrame frame, String str){
super(frame,str);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
public static void main(String[] args){
try{
RemoveMaxAndMinButton frame = new RemoveMaxAndMinButton(new JFrame(),
"Remove the Minimize and Maximize button from the Title Bar");
JPanel panel = new JPanel();
panel.setSize(200,200);
JLabel lbl = new JLabel("RoseIndia.Net");
panel.add(lbl);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
catch(IllegalArgumentException e){
System.exit(0);
}
}
}使其不可调整大小:
frame.setResizable(false);
在JFrame properties->maximumSize=minimumSize中,您仍然可以使用最小化和关闭按钮。。和resizeable=false。完成!该按钮已禁用。如果您正在使用Netbean,则只需取消选择“属性”中的“可调整大小”选项即可。它只会禁用最小化/最大化按钮。转到JFrame的属性并将resizeable设置为未选中。将type属性更改为utility。它只会显示关闭按钮。这可能会有所帮助:在我的mac电脑上,它会禁用
+
按钮。它真的删除了最大化按钮吗?在Linux上是这样的。。。在任何情况下,没有调整大小是OP想要实现的,对吗?我不是想说你发布了错误的东西。很有趣+无论如何,回答得很好。可能会禁用,但不会删除---只要删除“break”语句,删除“if”,并在必要时更新“if”。我使用此选项删除关闭按钮。只有当框架由LAFJDialog装饰时才可能,并且没有最小化按钮,这可能是问题,也可能不是问题。这不是没有最小化/最大化按钮的JFrame。它只是一个普通的JDialog
。你能再解释一下它的功能吗?你用的是JDialog而不是JFrame。JDialog和JFrame具有不同的行为,因此仅使用JDialog并不是解决这个问题的通用解决方案。
/**
* Removes the buttons from the JDialog title frame. This is a work around
* to removing the close button
*
* This is confirmed to work with the Metal L&F
*/
public void removeAllTitleFrameButtons() {
/* Get the components of the dialog */
Component[] comps = this.getRootPane().getComponents();
/* Indicator to break from loop */
boolean breakFromLoop = false;
/*
* Go through the components and find the title
* pane and remove the buttons.
*/
for(Component comp : comps) {
/* Shall we break from loop */
if(breakFromLoop) break;
if(comp.getClass().getName().indexOf("JLayeredPane") >0) {
for(Component jcomp : ((JLayeredPane)comp).getComponents()) {
if(jcomp.getClass().getName().indexOf("Title") > 0) {
/* Get the XXXXTitlePane Components */
Component[] titlePaneComps = ((JComponent)jcomp).getComponents();
for(Component tpComp : titlePaneComps) {
if(tpComp instanceof JButton) {
((JButton)tpComp).setVisible(false);
}
}
/* No need to continue processing */
breakFromLoop = true;
break;
}
}
}
}
}