我遇到了Java Swing应用程序的问题。
我的代码:
public class AdminToken extends JFrame {
VirtualServer server;
@Override
public Container getContentPane() {
return super.getContentPane();
}
public AdminToken(VirtualServer server)
{
this.server = server;
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout(5,1));
}
void setupGUI()
{
setTitle("Token Management Server: " + server.getName() );
setSize(576,528);
setVisible(true);
setResizable(true);
final JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,1));
LoginForm.api.selectVirtualServer(server);
for( final PrivilegeKey tokens : LoginForm.api.getPrivilegeKeys()) {
final JLabel label = new JLabel(tokens.getToken() + " " + tokens.getGroupId() + " " + "\n");
panel.add(label);
JButton button = new JButton("Löschen");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginForm.api.deletePrivilegeKey(tokens.getToken());
panel.remove(label);
panel.repaint();
}
});
button.setVisible(true);
button.setPreferredSize(new Dimension(87,25));
panel.add(button);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
LoginForm.api.selectVirtualServerById(LoginForm.vServerId);
}
标记应该显示如下:
Token1按钮删除\ n
令牌2按钮删除\ n
令牌3按钮删除\ n 所以一个
我也无法在另一个JFrame中更改按钮的大小:
守则:
public class TsServerOverview extends JFrame {
@Override
public Container getContentPane() {
return super.getContentPane();
}
public TsServerOverview()
{
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout());
}
void setupGUI()
{
setTitle("Ts3Query Server Oveview");
setSize(576,528);
setVisible(true);
setResizable(true);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,2));
for( final VirtualServer servers : LoginForm.api.getVirtualServers()) {
final int id = servers.getId();
panel.add(new JLabel(servers.getId() + " " + servers.getName() + " " + servers.getClientsOnline() + "/" + servers.getMaxClients() + "\n"));
JButton button = new JButton("Konsole öffnen");
JButton showchannels = new JButton("Channel-Liste");
JButton tokenmanagement = new JButton("Token Management");
showchannels.setPreferredSize(new Dimension(10,10));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GUIConsole(servers);
}
});
showchannels.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ChannelList(servers);
}
});
tokenmanagement.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AdminToken(servers);
}
});
tokenmanagement.setVisible(true);
button.setVisible(true);
showchannels.setVisible(true);
panel.add(showchannels);
panel.add(button);
panel.add(tokenmanagement);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
}
那也应该像这样显示:
Server1 3/32在线启动停止控制台\ n
Server2 3/32离线启动停止控制台\ n
Server3 3/32在线启动停止控制台
按钮应位于服务器线旁边。
如果有人可以帮我处理我的代码,那就太好了。
答案 0 :(得分:1)
public class AdminToken extends JFrame {
VirtualServer server;
@Override
public Container getContentPane() {
return super.getContentPane();
}
删除展示问题所不需要的所有内容。这包括VirtualServer
public AdminToken(VirtualServer server)
{
this.server = server;
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout(5,1));
}
getContentPane().setLayout
和setLayout
基本相同。没有使用它设置两次。避免空布局管理器。
void setupGUI()
{
setTitle("Token Management Server: " + server.getName() );
setSize(576,528);
setVisible(true);
setResizable(true);
不需要 setTitle
来证明这个问题。无需设定尺寸。在适用时设置首选大小,让布局管理器执行布局。 setResizable(true)
是默认值。这同样适用于button.setVisible(true);
您通常最后需要setVisible(true);
。
final JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,1));
按new GridLayout(5,1)
将网格设置为5行。如果添加更多,则会影响布局。来自java doc:
rows - 行,值为零表示任意行数。
LoginForm.api.selectVirtualServer(server);
for( final PrivilegeKey tokens : LoginForm.api.getPrivilegeKeys()) {
final JLabel label = new JLabel(tokens.getToken() + " " + tokens.getGroupId() + " " + "\n");
panel.add(label);
JButton button = new JButton("Löschen");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginForm.api.deletePrivilegeKey(tokens.getToken());
panel.remove(label);
panel.repaint();
}
});
button.setVisible(true);
button.setPreferredSize(new Dimension(87,25));
panel.add(button);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
LoginForm.api.selectVirtualServerById(LoginForm.vServerId);
}
以下mcve展示了我认为你想要实现的目标:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class AdminToken extends JFrame {
public AdminToken()
{
setupGUI();
setVisible(true);
setResizable(true);
pack();
}
void setupGUI() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0,2));
for(int row =0; row < 6; row++ ) {
panel.add(new JLabel("Label "+row));
panel.add(new JButton("Delete"));
}
JScrollPane scrollPane = new JScrollPane (panel);
add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new AdminToken() );
}
}