我在为其中一个元素(图像)设置位置时遇到问题。我无法将其对齐到屏幕的右下角。我尝试使用不同的布局,但我不能让它像我想要的那样工作。
以下是查看其现状的网址。图像位于右下角,但它是一个新的BorderLayout,它在底部创建了额外的空间,所以我希望它能与其他部分相匹配。红色方块显示应放置图像的位置。
以下是我的程序代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame implements ActionListener{
public static final int PORT = 8060;
private JButton reset, exit;
private JEditorPane messages;
private ImageIcon logo = new ImageIcon("C:src\\images\\logo.png");
private JLabel logoSpot;
Container box = getContentPane();
public static void main(String args[]){
new Server().Networking();
}
public void makeMenu(){
JPanel menu = new JPanel();
reset = new JButton("Reset messages");
exit = new JButton("Exit");
menu.add(reset);
menu.add(exit);
reset.addActionListener(this);
exit.addActionListener(this);
box.add(menu, BorderLayout.EAST);
}
public void setLogo(){
JPanel logoSpace = new JPanel(new BorderLayout());
logoSpot= new JLabel();
logoSpot.setIcon(logo);
logoSpace.add(logoSpot, BorderLayout.EAST);
box.add(logoSpace, BorderLayout.SOUTH);
}
public void makeScreen(){
JPanel screen = new JPanel();
messages = new JEditorPane();
messages.setPreferredSize(new Dimension(800,590));
screen.add(messages);
box.add(screen, BorderLayout.WEST);
}
public Server(){
makeMenu();
makeScreen();
setLogo();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(1024, 680);
setTitle("Emergency system");
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource() == exit){
super.dispose();
}
if(e.getSource() == reset){
messages.setText(" ");
}
}
public void Networking(){
String received=" ";
try{
ServerSocket ss = new ServerSocket(PORT);
while(true){
Socket sock = ss.accept();
BufferedReader in =
new BufferedReader(new InputStreamReader(
sock.getInputStream()));
received = in.readLine();
messages.setText(messages.getText() + received +
" \n\n");
OutputStreamWriter out =
new OutputStreamWriter(sock.getOutputStream());
BufferedWriter bw = new BufferedWriter(out);
bw.write(received);
bw.flush();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
答案 0 :(得分:4)
这应该可以解决问题。它只需要一些面板来将第二个图像(160x160px)约束到某些PAGE_END
实例的LINE_END
和BorderLayout
。
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame implements ActionListener{
public static final int PORT = 8060;
private JButton reset, exit;
private JEditorPane messages;
private ImageIcon logo = new ImageIcon(
new BufferedImage(140,140,BufferedImage.TYPE_INT_RGB));
private JLabel logoSpot;
Container box = getContentPane();
public static void main(String args[]){
new Server();
}
public void makeMenu(){
JPanel menu = new JPanel(new BorderLayout(5,5));
menu.setBackground(Color.RED);
reset = new JButton("Reset messages");
exit = new JButton("Exit");
JPanel buttons = new JPanel();
buttons.setBackground(Color.GREEN);
menu.add(buttons, BorderLayout.PAGE_START);
buttons.add(reset);
buttons.add(exit);
reset.addActionListener(this);
exit.addActionListener(this);
JLabel l = new JLabel(new ImageIcon(new BufferedImage(
160,160,BufferedImage.TYPE_INT_RGB)));
JPanel forceRight = new JPanel(new BorderLayout());
forceRight.add(l, BorderLayout.LINE_END);
forceRight.setBackground(Color.BLUE);
menu.add(forceRight, BorderLayout.PAGE_END);
box.add(menu, BorderLayout.EAST);
}
public void setLogo(){
JPanel logoSpace = new JPanel(new BorderLayout());
logoSpot= new JLabel();
logoSpot.setIcon(logo);
logoSpace.add(logoSpot, BorderLayout.EAST);
box.add(logoSpace, BorderLayout.SOUTH);
}
public void makeScreen(){
JPanel screen = new JPanel();
screen.setBackground(Color.YELLOW);
messages = new JEditorPane();
messages.setPreferredSize(new Dimension(800,590));
screen.add(messages);
box.add(screen, BorderLayout.WEST);
}
public Server(){
makeMenu();
makeScreen();
setLogo();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(1024, 680);
setTitle("Emergency system");
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource() == exit){
super.dispose();
}
if(e.getSource() == reset){
messages.setText(" ");
}
}
}
答案 1 :(得分:3)
您应该更好地组织代码的结构。构造函数通常在类的开头找到。
您可能拥有一个GUI组件,而不是有3个方法来创建GUI组件。您不需要使用getContentPane()方法。使用JFrame的add(...)方法时,组件将添加到内容窗格中。我还会使用JTextArea来显示消息。 JEdtitorPane只能用于HTML。您还应该将文本区域添加到JScrollPane,以便在添加更多消息时显示滚动条。
所以基本代码就像:
JTextArea textArea = new JTextArea(20, 50);
JScrollPane scrollPane = new JScrollPane( textArea );
add(scrollPane, BorderLayout.CENTER);
JPanel east = new JPanel( new BorderLayout() );
east.add(menuPanel, BorderLayout.NORTH);
east.add(logo, BorderLayout.SOUTH);
add(east, BorderLayout.EAST);