gpt4 book ai didi

java - 在 JPanel 中调整 JTextPane 的大小

转载 作者:行者123 更新时间:2023-12-04 02:44:40 25 4
gpt4 key购买 nike

我正在尝试创建一个 IM 程序作为一种业余爱好项目。我一直在试验 UI 设计,并一直在尝试让这个原型(prototype)用于实际的消息显示工作。

目前框架是这样设置的;

  • JFrame 内容 Pane 设置为 JPanel,它利用使用 Y_AXIS 的 BoxLayout。
  • 内容面板包含我想要显示的 TextMessage 对象,它们分别添加到内容面板。

TextMessage 对象是这样设置的;

  • 消息和发件人字符串存储在扩展 JTextPane 并使用 StyledDocument 进行格式化的 TextMessage 对象中。
  • TextMessage 放置在 JPanel 内部,以便在 BoxLayout 中正确放置对象。 JPanel 设置为 FlowLayout,它根据 boolean 值将 TextMessage 对象固定在 JPanel 的任一边缘。

到目前为止,除了一个明显的异常(exception),一切都按照我希望的方式进行。当我调整 JFrame 大小时,TextMessage 对象不会调整大小,而是简单地从屏幕边缘消失。

预期结果:

http://oi57.tinypic.com/nnl3bn.jpg

实际结果:

http://oi62.tinypic.com/2re7dbc.jpg

JFrame 类:

public class NewMain extends JFrame {

public NewMain() {
setTitle("SparrowIM Chat Bubble");
setPreferredSize(new Dimension(880, 550));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
setContentPane(panel);

add(new TextMessage("Hey, man. What's up?", "Jnk1296", true));
add(new TextMessage("Eh, nothing much.", "KeepJ96", false));
add(new TextMessage("Wbu? Anything new going on with you?", "KeepJ96", false));

add(new TextMessage("Nah, not really. Got a job interview coming up in a few " +
"days, though. Sorta excited for that, but sorta not. " +
"Other than that, life as usual. xD", "Jnk1296", true));

add(new TextMessage("lol Sounds good.", "KeepJ96", false));
add(new TextMessage("Yeah. How's the wife and kids, though?", "Jnk1296", true));
add(new TextMessage("Sarah still griping at you to get the roof patched up?", "Jnk1296", true));
add(new TextMessage("lol you could say that...", "KeepJ96", false));

pack();
setVisible(true);
}

public static void main(String[] args) {
new NewMain();
}
}

TextMessage 对象(TextMessageContent 的 JPanel 容器):

public class TextMessage extends JPanel {

private TextMessageContent content;

public TextMessage(String sender, String message, boolean localMessage) {
setBorder(new EmptyBorder(5, 5, 5, 5));

if (localMessage) {
setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
} else {
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
}

this.content = new TextMessageContent(sender, message, localMessage);
add(content);
}

public TextMessageContent getContent() {
return this.content;
}
}

TextMessageContent 类:(JTextPane)

public class TextMessageContent extends JTextPane {

private static final long serialVersionUID = -8296129524381168509L;
private String sender;
private String message;
private boolean isClient;

private Image background;

public TextMessageContent(String message, String sender, boolean isClient) {
// Define Data Points
this.message = message;
this.sender = sender;
this.isClient = isClient;

this.setEditable(false);
this.setOpaque(false);
this.setBorder(new EmptyBorder(7, 7, 7, 7));

// Fetch Background Image (Hard Location Temporary)
try {
if (this.isClient) {
background = ImageIO.read(
new File("/home/jacob/Desktop/orange.png"));
} else {
background = ImageIO.read(
new File("/home/jacob/Desktop/Green.png"));
}
} catch (Exception e) { return; }

// Create Text Styles
StyledDocument doc = getStyledDocument();

// Create Default Base Style
Style def = StyleContext.getDefaultStyleContext()
.getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("regular", def);

// Create Body Style
Style body = doc.addStyle("message", regular);
StyleConstants.setFontFamily(body, "Verdana");
StyleConstants.setFontSize(body, 12);

// Create Sender Style
Style foot = doc.addStyle("sender", body);
StyleConstants.setFontSize(foot, 9);

// Build Document
try {
doc.insertString(0, this.message + "\n", body);
doc.insertString(doc.getLength(), this.sender + " - " +
getSystemTime(), foot);
} catch (BadLocationException e) {
e.printStackTrace();
}
}

@Override
public void paintComponent(Graphics g) {
background = ImageUtil.stretch(background, new Insets(5, 5, 5, 5),
new Dimension(getWidth(), getHeight()),
BufferedImage.TYPE_INT_ARGB);

g.drawImage(background, 0, 0, null);
super.paintComponent(g);
}

private String getSystemTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
return sdf.format(cal.getTime());
}
}

我读过有关将 JTextPane 直接添加到容器等内容的信息,但是通过此设置,环绕文本消息内容的 JPanel 是必要的,以防止内容 Pane 的 BoxLayout 使文本消息填充整个面板。

有什么建议吗?

最佳答案

I've read about simply adding the JTextPane directly to the containers and whatnot, but with this set up, the JPanel wrapping around the text message content is necessary to keep the BoxLayout of the content pane from making the Text Messages fill the entire panel

不确定它是否有帮助,但 BoxLayout 尊重添加到它的组件的最大尺寸。也许您可以覆盖 getMaximumSize() 方法来控制宽度。宽度应为首选宽度或父容器宽度中的较小者。

关于java - 在 JPanel 中调整 JTextPane 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com