我在JPanel,JLabel和JButton中添加了2个元素.我希望它们彼此叠加,所以我使用BorderLayout.NORTH和SOUTH添加它们.
我遇到的问题是JLabel JButton并排坐在彼此旁边,而不是像预期的那样彼此叠加.这个问题可能与我使用borderlayout将其他面板添加到框架的事实有关,如下所示.
这是一个SSCE来演示我的问题.您会注意到,如果展开窗口,您将看到JLabel左侧的JButton.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Question {
/**
* @param args
*/
public static void main(String[] args) {
gui();
}
public static void gui() {
final JFrame theFrame = new JFrame();
theFrame.setSize(550, 290);
theFrame.setLocationRelativeTo(null);
// options for the JComboBox
String[] options = { ("1"), ("2"), ("3") };
final JPanel thePanel = new JPanel();
JLabel aMessage = new JLabel(
"<html>Lorem ipsum dolor …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的文本菜单,代码编译很好但执行冻结,因为它进入while循环并且循环内没有代码执行.代码很简单,我看不出问题.
void menu()
{
int option = 1;
while (option!=3);
{
printf("Select an option\n\n");
printf("1. Input data\n");
printf("2. View table of data\n");
printf("3. Exit program\n");
scanf("%d", &option);
switch (option)
{
case 1:
printf("Inputting data\n");
break;
case 2:
printf("Viewing table\n");
break;
default:
printf("Invalid choice, please try again\n");
break;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有下面的代码,它创建一个对象数组,然后尝试添加相同类型的对象.
我在行上得到错误"语法错误,插入"新的ClassType()"以完成表达式"
vehicles.[index] = nextVehicle;
Run Code Online (Sandbox Code Playgroud)
但在我看来,它没有任何问题.从长到int的演员阵容是否会造成一些问题?
long vehicleSize = getVehicleSize();
Vehicle[] vehicles= new Vehicle[(int)vehicleSize];
Vehicle nextVehicle = null;
int offSet = 0;
int index= 0;
while (offSet < vehicleSize) {
nextVeh = new Vehicle(db, offSet);
vehicles.[index] = nextVehicle;
index++;
offSet += nextVehicle.getSize();
}
Run Code Online (Sandbox Code Playgroud) 我已经读过类似的问题,但所提供的解决方案对我来说并不适用.
我想调用存在于另一个位于不同.cpp文件中的类的函数.我不想创建对象的实例,我只想使用该函数.
我的代码试图调用该函数:
switch (option)
{
case 1:
cout << "\nDoing stuff\n\n" ;
Controller::AlbumOps SayHey();
//SayHey should have run but isn't working
break;
Run Code Online (Sandbox Code Playgroud)
我试图调用的函数:
#include "Menu.hpp"
#include "Album.hpp"
#include "stdio.h"
#include "AlbumOps.hpp"
#include <iostream>
using namespace std;
namespace Controller
{
static void Controller::AlbumOps::SayHey ()
{
cout << "Hey\n";
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行代码时,永远不会打印嘿.我认为解决方案是使函数静态,但这对我没用.