我有一個JFrame,並想從中刪除最大化按鈕。如何從JFrame中刪除最大化按鈕?
我寫下面的代碼,但是它刪除了最大化,最小化,並且從我的JFrame
關閉了。
JFrame frame = new JFrame();
frame.add(kart);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setSize(400, 400);
我只想從JFrame
中刪除最大化按鈕。
我有一個JFrame,並想從中刪除最大化按鈕。如何從JFrame中刪除最大化按鈕?
我寫下面的代碼,但是它刪除了最大化,最小化,並且從我的JFrame
關閉了。
JFrame frame = new JFrame();
frame.add(kart);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setSize(400, 400);
我只想從JFrame
中刪除最大化按鈕。
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);
}
} }
你能解釋一下它的功能嗎? – Matthieu 2017-08-17 10:22:09
There描述瞭如何在不最大化和最小化按鈕的情況下實現「JFrame」。 你只需要 「incapsulate」 中的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);
}
}
}
這不是沒有最小化/最大化按鈕的'JFrame'。這只是一個普通的'JDialog'。 – Martin 2016-09-15 14:55:49
/**
* 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;
}
}
}
}
}
在JFrame屬性中 - > maximumSize = minimumSize。可調整大小= false。完成!該按鈕被禁用。
您是否在編輯器中? 在netbeans中不起作用... – 2017-07-23 09:14:40
如果您使用的是Netbean,那麼只需在屬性中取消選擇可調整大小的選項。它只會禁用最小化/最大化按鈕。
這*可能*幫助:http://geekycoder.wordpress.com/2009/07/17/java-tips-disabling-the-maximize-button-of-jframe/ – MByD 2011-04-11 18:05:57