JDialog显示问题

小贝壳666 2010-09-13 03:30:59

如上图 JDialog的小例子,运行时显示不完整(左侧图),但用鼠标托动一下,改变下大小,就会显示完整(如右图),请大虾们指教,怎么才能一下子显示完整。
代码如下:
                dialog.getContentPane().add(panel);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
dialog.setDefaultLookAndFeelDecorated(true);
dialog.setSize(180, 120);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
...全文
397 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyn1991121 2012-06-15
  • 打赏
  • 举报
回复
楼主怎么解决的??
小贝壳666 2010-09-13
  • 打赏
  • 举报
回复
解决了,谢谢各位
zaizaizaizaizai 2010-09-13
  • 打赏
  • 举报
回复
关注一下
小贝壳666 2010-09-13
  • 打赏
  • 举报
回复
不好意思,少一个JAVA类
第四个:
package com.yss.wz.panel;

/* (swing1.1.1beta2) */
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.metal.MetalTabbedPaneUI;


/**
* @version 1.1 06/02/99
*/
public class MnemonicTabbedPane extends JTabbedPane {
Hashtable mnemonics = null;
int condition;

public MnemonicTabbedPane() {
setUI(new MnemonicTabbedPaneUI());
mnemonics = new Hashtable();

// I don't know which one is more suitable for mnemonic action.
//setMnemonicCondition(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
setMnemonicCondition(WHEN_IN_FOCUSED_WINDOW);
}

public void setMnemonicAt(int index, char c) {
int key = (int)c;
if ('a' <= key && key <='z') {
key -= ('a' - 'A');
}
setMnemonicAt(index, key);
}

public void setMnemonicAt(int index, int keyCode) {
ActionListener action = new MnemonicAction(index);
KeyStroke stroke = KeyStroke.getKeyStroke(keyCode, ActionEvent.ALT_MASK);
registerKeyboardAction(action, stroke, condition);
mnemonics.put(new Integer(index), new Integer(keyCode));
}

public int getMnemonicAt(int index) {
int keyCode = 0;
Integer m = (Integer)mnemonics.get(new Integer(index));
if (m != null) {
keyCode = m.intValue();
}
return keyCode;
}

public void setMnemonicCondition(int condition) {
this.condition = condition;
}

public int getMnemonicCondition() {
return condition;
}

class MnemonicAction implements ActionListener {
int index;

public MnemonicAction(int index) {
this.index = index;
}

public void actionPerformed(ActionEvent e) {
MnemonicTabbedPane tabbedPane = (MnemonicTabbedPane)e.getSource();
tabbedPane.setSelectedIndex(index);
tabbedPane.requestFocus();
}
}

class MnemonicTabbedPaneUI extends MetalTabbedPaneUI {
protected void paintText(Graphics g, int tabPlacement,
Font font, FontMetrics metrics, int tabIndex,
String title, Rectangle textRect,
boolean isSelected) {
g.setFont(font);
MnemonicTabbedPane mtabPane = (MnemonicTabbedPane)tabPane;
if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
g.setColor(tabPane.getForegroundAt(tabIndex));
BasicGraphicsUtils.drawString(g,title, mtabPane.getMnemonicAt(tabIndex),
textRect.x,
textRect.y + metrics.getAscent());
} else {
g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
BasicGraphicsUtils.drawString(g,title, mtabPane.getMnemonicAt(tabIndex),
textRect.x,
textRect.y + metrics.getAscent());
g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
BasicGraphicsUtils.drawString(g,title, mtabPane.getMnemonicAt(tabIndex),
textRect.x - 1,
textRect.y + metrics.getAscent() - 1);
}
}
}
}

小贝壳666 2010-09-13
  • 打赏
  • 举报
回复
第三个:
package com.yss.wz.border;

/* (swing1.1.1) */
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


/**
* @version 1.0 08/12/99
*/
public class CompTitledBorder extends TitledBorder {
protected JComponent component;

public CompTitledBorder(JComponent component) {
this(null, component, LEFT, TOP);
}

public CompTitledBorder(Border border) {
this(border, null, LEFT, TOP);
}

public CompTitledBorder(Border border, JComponent component) {
this(border, component, LEFT, TOP);
}

public CompTitledBorder(Border border,
JComponent component,
int titleJustification,
int titlePosition) {
super(border, null, titleJustification,
titlePosition, null, null);
this.component = component;
if (border == null) {
this.border = super.getBorder();
}
}


public void paintBorder(Component c, Graphics g,
int x, int y, int width, int height) {
Rectangle borderR = new Rectangle(x + EDGE_SPACING,
y + EDGE_SPACING,
width - (EDGE_SPACING * 2),
height - (EDGE_SPACING * 2));
Insets borderInsets;
if (border != null) {
borderInsets = border.getBorderInsets(c);
} else {
borderInsets = new Insets(0, 0, 0, 0);
}

Rectangle rect = new Rectangle(x,y,width,height);
Insets insets = getBorderInsets(c);
Rectangle compR = getComponentRect(rect, insets);
int diff;
switch (titlePosition) {
case ABOVE_TOP:
diff = compR.height + TEXT_SPACING;
borderR.y += diff;
borderR.height -= diff;
break;
case TOP:
case DEFAULT_POSITION:
diff = insets.top/2 - borderInsets.top - EDGE_SPACING;
borderR.y += diff;
borderR.height -= diff;
break;
case BELOW_TOP:
case ABOVE_BOTTOM:
break;
case BOTTOM:
diff = insets.bottom/2 - borderInsets.bottom - EDGE_SPACING;
borderR.height -= diff;
break;
case BELOW_BOTTOM:
diff = compR.height + TEXT_SPACING;
borderR.height -= diff;
break;
}
border.paintBorder(c, g, borderR.x, borderR.y,
borderR.width, borderR.height);
Color col = g.getColor();
g.setColor(c.getBackground());
g.fillRect(compR.x, compR.y, compR.width, compR.height);
g.setColor(col);
component.repaint();
}



public Insets getBorderInsets(Component c, Insets insets) {
Insets borderInsets;
if (border != null) {
borderInsets = border.getBorderInsets(c);
} else {
borderInsets = new Insets(0,0,0,0);
}
insets.top = EDGE_SPACING + TEXT_SPACING + borderInsets.top;
insets.right = EDGE_SPACING + TEXT_SPACING + borderInsets.right;
insets.bottom = EDGE_SPACING + TEXT_SPACING + borderInsets.bottom;
insets.left = EDGE_SPACING + TEXT_SPACING + borderInsets.left;

if (c == null || component == null) {
return insets;
}

int compHeight = 0;
if (component != null) {
compHeight = component.getPreferredSize().height;
}

switch (titlePosition) {
case ABOVE_TOP:
insets.top += compHeight + TEXT_SPACING;
break;
case TOP:
case DEFAULT_POSITION:
insets.top += Math.max(compHeight,borderInsets.top) - borderInsets.top;
break;
case BELOW_TOP:
insets.top += compHeight + TEXT_SPACING;
break;
case ABOVE_BOTTOM:
insets.bottom += compHeight + TEXT_SPACING;
break;
case BOTTOM:
insets.bottom += Math.max(compHeight,borderInsets.bottom) - borderInsets.bottom;
break;
case BELOW_BOTTOM:
insets.bottom += compHeight + TEXT_SPACING;
break;
}
return insets;
}

public JComponent getTitleComponent() {
return component;
}

public void setTitleComponent(JComponent component) {
this.component = component;
}


public Rectangle getComponentRect(Rectangle rect,Insets borderInsets) {
Dimension compD = component.getPreferredSize();
Rectangle compR = new Rectangle(0,0,compD.width,compD.height);
switch (titlePosition) {
case ABOVE_TOP:
compR.y = EDGE_SPACING;
break;
case TOP:
case DEFAULT_POSITION:
compR.y = EDGE_SPACING +
(borderInsets.top -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
break;
case BELOW_TOP:
compR.y = borderInsets.top - compD.height - TEXT_SPACING;
break;
case ABOVE_BOTTOM:
compR.y = rect.height - borderInsets.bottom + TEXT_SPACING;
break;
case BOTTOM:
compR.y = rect.height - borderInsets.bottom + TEXT_SPACING +
(borderInsets.bottom -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
break;
case BELOW_BOTTOM:
compR.y = rect.height - compD.height - EDGE_SPACING;
break;
}
switch (titleJustification) {
case LEFT:
case DEFAULT_JUSTIFICATION:
compR.x = TEXT_INSET_H + borderInsets.left;
break;
case RIGHT:
compR.x = rect.width - borderInsets.right -TEXT_INSET_H -compR.width;
break;
case CENTER:
compR.x = (rect.width - compR.width) / 2;
break;
}
return compR;
}

}


问题出在用了CompTitledPane 怎么改还不清楚,请指教!!!
小贝壳666 2010-09-13
  • 打赏
  • 举报
回复
一共三个JAVA类

第一个:
/* (swing1.1.1beta2) */
//package jp.gr.java_conf.tame.swing.examples;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.yss.wz.panel.CompTitledPane;
import com.yss.wz.panel.MnemonicTabbedPane;

/**
* @version 1.1 zhouqh
*/
public class test extends JPanel {
public test() {
setLayout(new BorderLayout());
MnemonicTabbedPane tabbedPane = new MnemonicTabbedPane();
String[] tabs = { "1","2"};
for (int i = 0; i < tabs.length; i++) {
tabbedPane.addTab(tabs[i], createPane(tabs[i], String.valueOf(i)));
}
tabbedPane.setSelectedIndex(0);
add(tabbedPane, BorderLayout.CENTER);
JButton button = new JButton("保存设置");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {


final JDialog dialog = new JDialog(new JFrame(), "提示", true);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dialog.dispose();
}
});

JPanel panel = new JPanel();
JLabel text = new JLabel("设置成功!");
JButton b1 = new JButton("确定");

GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints constraints = new GridBagConstraints();
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.gridheight = 1;
panel.add(text, constraints);
constraints.gridy = 2;
panel.add(b1, constraints);
dialog.getContentPane().add(panel);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
dialog.setSize(160, 100);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
dialog.repaint();

}
});
add(button, BorderLayout.SOUTH);
}

JPanel createPane(final String s, String flag) {
final CompTitledPane p = new CompTitledPane();
return p;
}


public static void main(String[] args) {

JFrame frame = new JFrame("123");
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.add(new test());
frame.setSize(480, 280);
frame.setLocationRelativeTo(null); // 窗口启动时置于屏幕中间
frame.setVisible(true);
}
}


第二个:
package com.yss.wz.panel;

/* (swing1.1.1) */
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.yss.wz.border.CompTitledBorder;

/**
* @version 1.0 08/12/99
*/
public class CompTitledPane extends JPanel {
protected CompTitledBorder border;
protected JComponent component;
protected JPanel panel;
protected boolean transmittingAllowed;
protected StateTransmitter transmitter;

public CompTitledPane() {
this(new JLabel("Title"));
}

public CompTitledPane(JComponent component) {
this.component = component;
border = new CompTitledBorder(component);
setBorder(border);
panel = new JPanel();
//setLayout(null);
add(component);
add(panel);
transmittingAllowed = false;
transmitter = null;
}

public JComponent getTitleComponent() {
return component;
}

public void setTitleComponent(JComponent newComponent) {
remove(component);
add(newComponent);
border.setTitleComponent(newComponent);
component = newComponent;
}

public JPanel getContentPane() {
return panel;
}

public void doLayout() {
Insets insets = getInsets();
Rectangle rect = getBounds();
rect.x = 0;
rect.y = 0;

Rectangle compR = border.getComponentRect(rect,insets);
component.setBounds(compR);
rect.x += insets.left;
rect.y += insets.top;
rect.width -= insets.left + insets.right;
rect.height -= insets.top + insets.bottom;
panel.setBounds(rect);
}


public void setTransmittingAllowed(boolean enable) {
transmittingAllowed = enable;
}

public boolean getTransmittingAllowed() {
return transmittingAllowed;
}

public void setTransmitter(StateTransmitter transmitter) {
this.transmitter = transmitter;
}

public StateTransmitter getTransmitter() {
return transmitter;
}

public void setEnabled(boolean enable) {
super.setEnabled(enable);
if (transmittingAllowed && transmitter != null) {
transmitter.setChildrenEnabled(enable);
}
}

}


huntor 2010-09-13
  • 打赏
  • 举报
回复
就楼主你有问题,别人都正常显示了。
youjianbo_han_87 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gardner2010 的回复:]

JFrame jframe = new JFrame();
dialog.repaint();
都试了,还是不行啊
[/Quote]

将完整代码(包括测试代码) 发上来看看。。
qingtianliuyun 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tassdars 的回复:]
我拿你的代码跑了一下,只在前面加了一句JFrame jframe = new JFrame();

然后一点问题也没有,可以正常显示,我搞不懂了。
[/Quote]
跟4楼一样,我也试了,没有问题。
你看是不是你的jframe等其他相关代码的问题
小贝壳666 2010-09-13
  • 打赏
  • 举报
回复
JFrame jframe = new JFrame();
dialog.repaint();
都试了,还是不行啊
linyuzhi 2010-09-13
  • 打赏
  • 举报
回复
我在我机子上试了一下,没有你说的问题啊!

你加入这一条试试,刷新一下。
dialog.repaint();
Tassdars 2010-09-13
  • 打赏
  • 举报
回复
我拿你的代码跑了一下,只在前面加了一句JFrame jframe = new JFrame();

然后一点问题也没有,可以正常显示,我搞不懂了。
小贝壳666 2010-09-13
  • 打赏
  • 举报
回复
沉了,没人会啊?
小贝壳666 2010-09-13
  • 打赏
  • 举报
回复
谢谢楼上的:完整代码如下
                final JDialog dialog = new JDialog(jframe, "提示", true);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dialog.dispose();
}
});

JPanel panel = new JPanel();
JLabel text = new JLabel("设置成功!");
JButton b1 = new JButton("确定");

GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints constraints = new GridBagConstraints();
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.gridheight = 1;
panel.add(text, constraints);
constraints.gridy = 2;
panel.add(b1, constraints);
dialog.getContentPane().add(panel);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
dialog.setDefaultLookAndFeelDecorated(true);
dialog.setSize(180, 120);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);


不是Size的问题,设大了显示也不完整,还是要用鼠标拖一下才能显示。
Tassdars 2010-09-13
  • 打赏
  • 举报
回复
那你一开始就把JDialog的size设大点不行吗?而且代码不完整,不知道你new了dialog又干了啥,下回记得贴完整代码,别人也好调

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧